Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-30
6
Medium Priority
?
337 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 111

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 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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: 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.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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…

704 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