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
Solved

finally unexpected {

Posted on 2014-04-07
3
873 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 109

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 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