views:

107

answers:

2

I'm attempting to throw an exception using php 5.3.2 and it is giving me the following error:

Parse error: syntax error, unexpected T_THROW

I am attempting to throw an exception the following way:

throw new Exception('Property ' . $name . ' doesn\'t exist in class Index', '');

Edit: I also tried

throw new Exception('Property ' . $name . ' doesn\'t exist in class Index');

it didn't change the error I was getting.

The complete method:

public function __get($name) 
    {
        if(property_exists($this, $name)
            throw new Exception('Property ' . $name . ' doesn\'t exist in class Index');
        return $this->$name;
    }
+1  A: 

check your php code for bloopers, sometimes i miss off a ';' which can cause errors.

also try writing the if statement formally, adding in brackets. i know it shouldn't make any odds but who knows with programming!

Rob
son-of-a-gun, you made you go through the method and realize i left off a paranthesis.. thanks.
tipu
@tipu since your accept rate is quite low, here is a friendly reminder to mark this message accepted if it solved your problem.
Gordon
hehe, no problem, if i had a £1 for every time that happened to me, i'd be rich!
Rob
A: 

You are missing the closing parenthesis of your if() line and so PHP sees this

if(property_exists($this, $name) throw new Exception(...);

which is not valid syntax. Place a ) at the end of your if() line:

if(property_exists($this, $name))

Edit: I hate when I miss the replies below an answer. :(

David Harkness