Solved

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

Posted on 2014-03-30
6
320 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
Technology Partners: 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get data from two MySQL tables 6 66
Form submit takes only for one form 23 53
phpmyadmin memory error 55 94
Ajax success not firing alert 6 42
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 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