Syntax Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Syntax Error

Post 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
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Syntax Error

Post 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__)));
    }
           
}
 
?>
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Syntax Error

Post by RobertGonzalez »

That is exactly right. Default properties can only be set with simple expressions. Functions calls will make it fail every time.
Post Reply