Solved

finally unexpected {

Posted on 2014-04-07
3
857 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
3 Comments
 
LVL 34

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now