is class exception parent of class xmlexception extends exception, and when does code break

<?php

class XmlException extends Exception {
    private $error;

    function __construct( LibXmlError $error ) {
        $shortfile = basename( $error->file );
	print_r( $error );
        $msg = "[{$shortfile}, line# {$error->line}, col# {$error->column}] {$error->message}";
        $this->error = $error;
        parent::__construct( $msg, $error->code );
    }

    function getLibXmlError() {
        return $this->error;
    }
}

class FileException extends Exception { }
class ConfException extends Exception { }

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

    function __construct( $file ) {
        $this->file = $file;
        if ( ! file_exists( $file ) ) {
            throw new FileException( "file '$file' does not exist" );
        }
        $this->xml = simplexml_load_file($file, null, LIBXML_NOERROR );
        if ( ! is_object( $this->xml ) ) {
            throw new XmlException( libxml_get_last_error() );
        }
        $matches = $this->xml->xpath("/conf");
        if ( ! count( $matches ) ) {
            throw new ConfException( "could not find root element: conf" );
        }
    }    

    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 );
    }
}


class Runner {
    static function init() { 
        try {
            //$error_test= new XmlException(1);
            //$conf = new Conf( dirname(__FILE__)."/conf01-edit.xml" );
            $conf = new Conf( dirname(__FILE__)."/conf.broken.xml" );
            $conf = new Conf( dirname(__FILE__)."/fake.xml" );
            print "user: ".$conf->get('user')."\n";
            print "host: ".$conf->get('host')."\n";
            $conf->set("pass", "newpass");
            $conf->write();
        } catch ( FileException $e ) {
            // permissions issue or non-existent file
            throw $e;
        } catch ( XmlException $e ) {
            // broken xml
        } catch ( ConfException $e ) {
            // wrong kind of XML file
        } catch ( Exception $e ) {
            // backstop: should not be called
        }
    }
}

Runner::init();

?>

Open in new window


LibXMLError Object ( [level] => 3 [code
] => 76 [column] => 12 [message] => Opening and ending tag mismatch: conf-broked line 2 and conf [file] => file:///C:/wamp/www/POPP-edition4-code/9781430260318_Chapter_04_Code/conf.broken.xml [line] => 6 )


when does this code break?
does code break after running
parent::__construct( $msg, $error->code );
is class exception parent of class xmlexception extends exception
after running line11
debugger goes to line12 and breaks
I think I know why error
why did code stop at that line
LVL 1
rgb192Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Julian HansenConnect With a Mentor Commented:
Did not read your question properly before.

This is what is happening
- The code is running to line 74.
- The code attempts to read the broken xml file on line 32.
- On line 33 the return from 32 is tested to see if it is valid but fails
- The code drops to line 34 and throws an XmlException passing in the last error encountered by lib XML.
- This results in the instantiation of the XmlException object - and where your print_r($error) line executes
- The code then drops into the catch statement on line 84 - at this point you could do the following to your catch block
        } catch ( XmlException $e ) {
           $error = $e->getLibXmlError();
           echo "Exception caught: " . $error->message . 'in file : ' . $error->file . ' on line ' . $error->line ;
            // broken xml
        }

Open in new window

You can also make your error dump more informative by adding <pre> tags around it i.e.
echo "<pre>";
print_r($error);
echo "</pre>";

Open in new window

Which should output something like this
LibXMLError Object
(
    [level] => 3
    [code] => 5
    [column] => 56
    [message] => Extra content at the end of the document

    [file] => file:///G:/Projects/Sandbox/website/ee/broken.xml
    [line] => 6
)

Open in new window


The reason your debug stops on line 12 is that the exception is instantiated but you don't do anything with it in the catch block so there is nothing more to do.

By adding the code above to the catch block you should see the code terminate on or around line 87
0
 
Julian HansenCommented:
Code is breaking on line 76
LibXMLError Object ( [level] => 3 [code
] => 76 [column] => 12 

Open in new window

Error is caused by a problem in the xml file you are using.
0
 
Ray PaseurCommented:
is class exception parent of class xmlexception extends exception
Yes.
why did code stop at that line
Line 74 creates a new Conf object in the variable named $conf.  Line 75 overwrites variable $conf with another new Conf object.  Line 76 does not appear to be the problem (I think the "76" came from an error code, not a line number).  I believe line 34 threw a new XmlException using the data in the PHP function libxml_get_last_error().  This happened because of the creation of a new Conf object on line 74, where the XML document is not valid.  From the document name, it appears that the XML is intentionally not valid.

Executive summary: The code did not "break" at all.  Line 11 called the Exception constructor.  This was perfectly normal and expected.

Maybe it's the IDE that has trouble following the logic here?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
rgb192Author Commented:
line 11
 parent::__construct( $msg, $error->code );

is run

is this the 'error class'  which is not visible by my ide

ide waits 10 seconds and then runs line 12
}

and then stops

maybe finished the 'error child' class
0
 
Ray PaseurCommented:
I don't know.  When you have layers of IDEs and example code, anyone's guess is as good as mine.  Anything that waits 10 seconds then resumes is a strange design.
0
 
rgb192Author Commented:
is class exception parent of class xmlexception extends exception
0
 
rgb192Author Commented:
thanks for information to dump variable values.
0
 
Julian HansenCommented:
You are welcome - thanks for the points.
0
All Courses

From novice to tech pro — start learning today.