Solved

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

Posted on 2014-03-30
6
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 110

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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