Re: Simple class declaration returns error [SOLVED]

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

 



Dave M G wrote:
> Jochem,
> 
> Thank you for your advice. I was able to use your suggested code to
> develop the results I wanted.
> 
> I found I could simplify the "self" references down to just the name of
> the class.

that sounds backwards - self == 'database' with the added bonus that if you
change tyhe class name you don't have to change the code inside the class
because self == 'yournewclassname'; not a deal breaker though.

another tip (going on the code you showed here): use indentation, and
use it consequently - it helps code readability/maintainability

> 
> The syntax I ended up with is
> 
> class database {
> private static $database;
> public static function getDB()
> {
> if (!isset($database))
> $database = new database();
> return $database;
> }
> private function __construct()
> {
>   $this->dbConnect();
> }
> private function dbConnect()
> {
> // Database connection
>   return $result;
> }
> public function getData($type, $id)
> {
> // SQL Query
> return $data;
> }
> 
> 
> And I call it like this:
> $data = database::getDB()->getData($arg1, arg2);

looks good; you might consider doing this instead:

// 'davedb' is short than 'database' :-)
class davedb {
	// all the stuff from your class example here

	public static function gd($type, $id)
	{
		return self::getDB()->getData($type, $id);
	}
}

which allows for writing a little less code e.g.:

	$data = davedb::gd(arg1, arg2);

my function names may not be any good but hopefully you get the idea.

> 
> Thank you for your time and advice.

no worries :-)

> 
> -- 
> 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