Попробуйте это:
class MyPdoSingleton {
protected $pdo;
// Your own constructor called the first time if the singleton
// instance does not exist
protected function __construct() {
}
// Always returns the same instance (singleton)
public static function getInstance() {
static $instance;
return (is_object($instance)) ? $instance : $instance = new self();
}
// Redirect any non static methods calls made to this class to the contained
// PDO object.
public function __call($method, $args) {
return call_user_func_array(array($this->pdo, $method), $args);
}
// 5.3+
public function __callStatic($method, $args) {
$inst = self::getInstance();
return call_user_func_array(array($inst, $method), $args);
}
// Or if you intend to have other classes inherit from this one
/*public function __callStatic($method, $args) {
$class = get_called_class();
$inst = call_user_func(array($class, 'getInstance'));
return call_user_func_array(array($inst, $method), $args);
}*/
public function myOtherMethod($arg) {
// __call would not get called when requesting this method
}
}
// Pre 5.3 use
$db = MyPdoSingleton::getInstance();
$db->myOtherMethod();
// Post 5.3 use
MyPdoSingleton::myOtherMethod();
Ops.Я полностью испортил это.Вот что я получаю, отвечая на вопросы первым делом утром.