[nycphp-talk] Accessing a method from one class in another
David Mintz
dmintz at davidmintz.org
Tue Jan 17 15:59:51 EST 2006
If you're using PHP 5, one nice thing about the good old singleton is you
can get your instance from any scope.
$db = MyDatabaseClass::getInstance() ;
//// from MyDatabaseClass
private static $instance = null;
public static function getInstance() {
if (is_null(self::$instance)) {
// this uses PEAR and relies on an __autoload
self::$instance = DB::connect("$driver://$user:$password@$host/$db");
if (DB::isError(self::$instance)){
trigger_error('could not connect to database',E_USER_ERROR);
}
self::$instance->setFetchMode(DB_FETCHMODE_ASSOC);
// et cetera
}
return self::$instance;
}
---
David Mintz
http://davidmintz.org/
Amendment IV
The right of the people to be secure in their
persons, houses, papers, and effects, against
unreasonable searches and seizures, shall not be
violated, and no Warrants shall issue, but upon
probable cause, supported by Oath or affirmation,
and particularly describing the place to be
searched, and the persons or things to be seized.
More information about the talk
mailing list