Re: Simple class declaration returns error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux