Solved

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

Posted on 2014-03-30
6
294 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 108

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 33

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 108

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 108

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now