简体   繁体   English

PHP5.3:如何从主源代码使用 PDO singleton class(间接)

[英]PHP5.3 : How to use PDO singleton class (indirectly) from the main source code

Using PHP5.3.3, I have 2 classes, called SinglePDO and Manager , both working, but I'd certainly need your advices to optimize those unefficient code scriptings.使用 PHP5.3.3,我有 2 个类,称为SinglePDOManager ,它们都可以工作,但我当然需要你的建议来优化那些低效的代码脚本。 Hence I have 2 questions but I guess strictly related one to another:因此,我有 2 个问题,但我想彼此之间严格相关:


1) Access to the SinglePDO methods from a Manager object in the main code 1) 从主代码中的Manager object 访问SinglePDO方法

From the main code:从主要代码:

$test=new Manager('mydbname', $some_parameters);
$dbfields = $test->getFieldsSummary(false);

In the Manager class, redefined function, provided it's defined in the SinglePDO class:管理器class 中,重新定义 function,前提是它在SinglePDO class 中定义:

public function getFieldsSummary($param)
{
    return $this->_dbh->getFieldsSummary($param);
}

Question:问题:
How to get rid of the necessity to redefine all SinglePDO methods in the Manager class?如何摆脱在Manager class 中重新定义所有SinglePDO方法的必要性? (I have tried: class Manager extends SinglePDO and using: __call() but w/o success) (我试过: class Manager 扩展 SinglePDO并使用:__call ()但没有成功)


2) Split SinglePDO into 2 classes 2)将 SinglePDO拆分为 2 类
The SinglePDO class embeds lots of methods. SinglePDO class 嵌入了很多方法。 I'd like to unpack these methods and throw them into another class, say Tool overloading the SinglePDO class with method 1, method_2, etc...我想解压这些方法并将它们放入另一个 class,例如使用方法 1、method_2 等重载SinglePDO class 的工具...

Question:问题:
How to achieve this in the previous context (still having the Manager Class)如何在前面的上下文中实现这一点(仍然有 Manager 类)


Here is the SinglePDO class, a typical one but with additional methods.这是SinglePDO class,一个典型的,但有额外的方法。

       class SinglePDO extends PDO {
            private static $_dsn    = 'mysql:host=127.0.0.1;dbname=foobar';
            private static $_dbuser= 'dbuser' ;
            private static $_dbpwd  = 'dbpwd' ;
            private static $_lock = true;

        public function __construct( $dsn , $uname, $upass ) {

           if( self::$lock ) {
            trigger_error( 'Forbidden Class usage (singleton)');
           }
           parent::__construct( $dsn , $uname, $upass  );

        }

    public static function getInstance($dbh) {
        if( self::$instance == NULL ){
            self::$lock= FALSE;
                    self::$instance = new SinglePDO(self::$_dsn, self::$_dbuser, self::$_dbpwd);
                    self::$lock = TRUE;
        }
    return self::$instance;
    }

    /* lots of added methods that I'd like to drop into a Tool class  */
    public function my_method_1($param) {
     // do this and that
    }
} /* end of the uggly class */

EDIT :编辑
In btwn, I found this very interesting link在 btwn 中,我发现了这个非常有趣的链接

For the first question, did you use call in conjunction with call_user_func_array?对于第一个问题,您是否将 call 与 call_user_func_array 结合使用? Because this has worked for me in projects before:因为这在以前的项目中对我有用:

function __call( $fname, array $fargs )
{
    return call_user_func_array( array( $this->_dbh, $fname ), $fargs );
}

To question two: Well, if you get an answer to question 1 you will be able to get an answer to this as well.问题二:好吧,如果你得到了问题 1 的答案,你也将能够得到这个问题的答案。 I might have something like:我可能有类似的东西:

class PDOTool
{
    /* why does getInstance have a parameter? */
    private static $pdo = SinglePDO::getInstance(NULL);

    public static my_method_1($param) {
       self::$pdo->doSomething($param);
    }
    /**  __callStatic works in PHP >= 5.3.0  */
    public function __callStatic( $fname, array $fargs )
    {
        return call_user_func_array( array( self::$pdo, $fname ), $fargs );
    }
}

Answer to question #1: If you extend a class with another, all public and protected methods will be available in the sub-class without re-defining them.对问题 #1 的回答:如果您使用另一个扩展 class,则所有publicprotected的方法都将在子类中可用,而无需重新定义它们。

Answer to question #2 is to extend the classes one after another.问题 #2 的答案是一个接一个地扩展类。

class Tools extends SinglePDO {}

class Manager extends Tools {}

BTW: weird implementation of a singleton pattern in PHP .顺便说一句:在 PHP 中奇怪地实现了 singleton 模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM