Solved

finally unexpected {

Posted on 2014-04-07
3
834 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
Comment Utility
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
Comment Utility
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
Comment Utility
I am using 5.4

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Trouble parsing soap xml result 3 26
How do I edit this Drupal page? 9 26
Creating a slider 12 32
Strip leading 0 from a var 3 6
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

772 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

8 Experts available now in Live!

Get 1:1 Help Now