Page 1 of 1

Syntax Error

Posted: Mon Jun 02, 2008 7:28 pm
by SidewinderX
Not sure why this code is giving me an error. What am I missing?
Parse error: syntax error, unexpected '(', expecting ')' in /home/john/www/dev/config.php on line 8

Code: Select all

<?php
 
class Config {
 
    private $paths = 
        array(
            'root' => realpath(dirname(__FILE__))
        );
            
}
 
?>
Thanks,
John

Re: Syntax Error

Posted: Mon Jun 02, 2008 10:55 pm
by nowaydown1
I'm pretty sure that PHP isn't going to let you initialize that private member variable with a function call in it. Just initialize your private member, then make your function call in your constructor. I think that should work for you:

Code: Select all

 
<?php
 
class Config {
 
    private $paths = array();
    
    public function __construct() {
        $this->paths = array('root' => realpath(dirname(__FILE__)));
    }
           
}
 
?>
 

Re: Syntax Error

Posted: Mon Jun 02, 2008 11:04 pm
by RobertGonzalez
That is exactly right. Default properties can only be set with simple expressions. Functions calls will make it fail every time.