Solved

where is the set.  xml file not modified, output not modified

Posted on 2014-03-30
6
312 Views
Last Modified: 2014-03-31
from object oriented php tutorial

<?php

class Conf {
    private $file;
    private $xml;
    private $lastmatch;

    function __construct( $file ) {
        $this->file = $file;
        if ( ! file_exists( $file ) ) {
            throw new Exception( "file '$file' does not exist" );
        }
        $this->xml = simplexml_load_file($file);
    }    

    function write() {
        if ( ! is_writeable( $this->file ) ) {
            throw new Exception("file '{$this->file}' is not writeable");
        }
        file_put_contents( $this->file, $this->xml->asXML() );
    }

    function get( $str ) {
        $matches = $this->xml->xpath("/conf/item[@name=\"$str\"]");
        if ( count( $matches ) ) {
            $this->lastmatch = $matches[0];
            return (string)$matches[0];
        }
        return null;
    }

    function set( $key, $value ) {
        if ( ! is_null( $this->get( $key ) ) ) {
            $this->lastmatch[0]=$value;
            return;
        }
        $conf = $this->xml->conf;
        $this->xml->addChild('item', $value)->addAttribute( 'name', $key );
    }
}


try {
    $conf = new Conf( dirname(__FILE__)."/conf01.xml" );
    //$conf = new Conf( dirname(__FILE__)."/conf.unwriteable.xml" );
    //$conf = new Conf( "nonexistent/not_there.xml" );
    print "user: ".$conf->get('user')."\n";
    print "host: ".$conf->get('host')."\n";
    $conf->set("pass1", "newpass1");
    $conf->write();
} catch ( Exception $e ) {
    die( $e->__toString() );
}

?>

Open in new window

output
user: bob host: localhost

line 49
originally
    $conf->set("pass", "newpass");
now
    $conf->set("pass1", "newpass1");
where is the set.  I do not see change in output or ide local,global variables and xml file not modified
0
Comment
Question by:rgb192
  • 3
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39966224
What is the point of this code?  It has no comments and it uses private properties so you can't extend it.  What is it supposed to do, in layman's terms?
0
 

Author Comment

by:rgb192
ID: 39966777
It is from Matt Z book. Objects patterns and practice 4

Explanation in book only is about try and catch


My question is only about the set.
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 400 total points
ID: 39967493
You ask - "where is the set" - - - ???, the set( ) function is clearly labeled in the code, in case you can not find it -

function set( $key, $value ) {
  if ( ! is_null( $this->get( $key ) ) ) {
    $this->lastmatch[0]=$value;
     return;
     }
  $conf = $this->xml->conf;
  $this->xml->addChild('item', $value)->addAttribute( 'name', $key );
  }

= = = =
you also say - "I do not see change in output " ? ? ?  Did you take the time to analyze the code work here, and try and understand what this is doing, and WHY he placed the methods in this for - get( ), set( ) , and write( ),  ? ? Look at the code - the only output from this is generated by the TWO get( ) methods printed as -
      print "user: ".$conf->get('user')."\n";
      print "host: ".$conf->get('host')."\n";

So your question makes no sense?, if you input additional info by the  set( ) method, IT does not do any output! ! As a matter of fact there is no print or echo ANYWHERE in this class.

Please look at the code to see if you can figure out how to output any values you use set( ) to place in this.

failing your understanding of this, then try these lines --

    print "user: ".$conf->get('user')."\n";
    print "host: ".$conf->get('host')."\n";
    $conf->set("NewValue", "Have FUN");
    print ", NewValue: ".$conf->get('NewValue')."\n";
    $conf->set("NewValue", "Something ELSE on second SET");
    print ", NewValue number 2: ".$conf->get('NewValue')."\n";

= = = = = = = =
the entire point of this Class seems to be the  try and catch thing in it, You are suppose to try these lines -
$conf = new Conf( "nonexistent/not_there.xml" );
so you see what happens for the try and catch in this thing.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39967535
See line 32.  Set() is a member function (also called a method) of the class Conf.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 39967541
Please see line 32 of the code snippet.  Set() is a member function (also called a "method") of the Conf class.
0
 

Author Closing Comment

by:rgb192
ID: 39967775
I could not understand that the code adds to object (I can see in ide) and code writes to xml.

   Slick812 showed me lines to add.

Thanks both.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question