Php 如何将公共构造函数变成子类中的受保护构造函数?

Php 如何将公共构造函数变成子类中的受保护构造函数?,php,singleton,pdo,visibility,Php,Singleton,Pdo,Visibility,我试图扩展PDO类,并将其转换为单例。唯一的问题是PDO的构造函数是公共的,PHP不允许我将其作为受保护的方法重写。有没有什么富有想象力的方法?如果我尝试这样做,我会永远被这个松散的结局困住吗?另一种方法可能不是扩展PDO,而是将其保存在静态属性中,并对其进行操作,但我希望我的类尽可能保留PDO的所有功能。您可以将PDO类包装在自己的“Singleton Factory”对象中。基本上,您可以实现自己的包含(单个)PDO实例的单例。(请注意,我不知道PHP语法,所以这是Java,但您应该能够理解

我试图扩展PDO类,并将其转换为单例。唯一的问题是PDO的构造函数是公共的,PHP不允许我将其作为受保护的方法重写。有没有什么富有想象力的方法?如果我尝试这样做,我会永远被这个松散的结局困住吗?另一种方法可能不是扩展PDO,而是将其保存在静态属性中,并对其进行操作,但我希望我的类尽可能保留PDO的所有功能。

您可以将PDO类包装在自己的“Singleton Factory”对象中。基本上,您可以实现自己的包含(单个)PDO实例的单例。(请注意,我不知道PHP语法,所以这是Java,但您应该能够理解)

这里可以找到更详细的解释:

(再说一次,Java…对不起-但我相信它会让你达到你想去的地方)

试试以下方法:

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();

行动计划。我完全搞砸了。这是我早上第一件回答问题的事情。

这很好,让我思考,但唯一的问题是我还想用其他方式修改PDO类(添加方法)。我想我可以扩展PDO来添加方法,然后为扩展的类创建一个singleton factor类however.hmm。这也是一个有趣的想法。使用magic method_u_调用调用包含的pdo对象上的其他方法。现在我有一些决定要做。如果你使用5.3,你也可以使用静态调用魔法方法,这样你就不必一直调用getInstance。本质上,你会有在非静态环境下工作的静态方法。我在写了最后一条评论后意识到,它没有很好地解释我的意思。我扩展了我的示例类,添加了注释,添加了用例示例。看看我对的回答,这与你面临的问题差不多。
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();