Solved

finally unexpected {

Posted on 2014-04-07
3
886 Views
Last Modified: 2014-04-08
from matt zandstra objects patterns and practice php book

<?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();

?>

Open in new window


Parse error: syntax error, unexpected '{' in C:\wamp\www\POPP-edition4-code\9781430260318_Chapter_04_Code\listing4.21.php on line 92
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 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39985157
From the manual:
In PHP 5.5 and later, a finally block may also be specified after the catch blocks

Are you using PHP 5.5 or later?

HTH,
Dan
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39985587
Please see the RFC.  This is relatively new in PHP.
https://wiki.php.net/rfc/finally

Some other references worth exploring:
http://www.php.net/manual/en/language.exceptions.extending.php
http://www.php.net/manual/en/language.exceptions.php

If you're using PHP <5.5 you have to think of try / catch as if it were a series of if() statements.  You might try omitting the finally keyword.  This is not the same logic as it would be with finally, but it will at least parse correctly.

Some programmers, myself and Joel among them, do not think very much of the whole idea of exception handling.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

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

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39986961
I am using 5.4

and I like Ray's finally fix.
Putting code at the end of the block.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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