rgb192
asked on
finally unexpected {
from matt zandstra objects patterns and practice php book
Parse error: syntax error, unexpected '{' in C:\wamp\www\POPP-edition4- code\97814 30260318_C hapter_04_ Code\listi ng4.21.php on line 92
<?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() {
$fh = fopen("./log.txt","w");
try {
fputs( $fh, "start\n" );
$conf = new Conf( dirname(__FILE__)."/conf.not-there.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
fputs( $fh, "file exception\n" );
throw $e;
} catch ( XmlException $e ) {
fputs( $fh, "xml exception\n" );
// broken xml
} catch ( ConfException $e ) {
fputs( $fh, "conf exception\n" );
// wrong kind of XML file
} catch ( Exception $e ) {
fputs( $fh, "general exception\n" );
// backstop: should not be called
} finally {
fputs( $fh, "end\n" );
fclose( $fh );
}
}
}
Runner::init();
?>
Parse error: syntax error, unexpected '{' in C:\wamp\www\POPP-edition4-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
and I like Ray's finally fix.
Putting code at the end of the block.