Re: Simple class declaration returns error

From: Date: Thu, 15 Jun 2006 07:18:37 +0000
Subject: Re: Simple class declaration returns error
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-238047@lists.php.net to get a copy of this message
Dave M G wrote: > PHP List, > > Okay, I've upgraded to php 5 on my home machine, and I'm still getting what version? > some syntax errors. > > Parse error: syntax error, unexpected T_NEW in > /home/dave/web_sites/thinkingworks.com/web/database.class on line 5 > > This is the code producing the error: > > class database { > public static $database = new database; you can only set literal or constant values to properties of the class in the actual definition. and there is a school of thought that says you shouldn't be setting any values in the definition: class database { private static $database; public static function getDB() { if (!isset(self::$database)) self::$database = new self; return self::$database; } public function query($sql, $args = array()) { // query magic here } } // now you can do something like: database::getDB()->query("SELECT * FROM foo WHERE id=?", array($id)); notice that the static member variable is no longer accessible from the outside and therefore it is not possible for any code other that the database class to actually overwrite that variable. > > I've also tried: > > private static $database = new database; > > and: > > private static $database = new database; > > and: > > public static $database = 'database'; this last one should work. as should: public static $database = array(1,2,3); public static $database = 1; public static $database = MY_GLOBAL_CNST; > > The last of which I copied directly from the PHP manual. > > I'm just starting out with objects this week, so I know I am missing a > very basic thing. If anyone can nudge me in the right direction, that > would be fantastic. > > Thank you for your advice. > > -- > Dave M G

« previous php.general (#238047) next »