简体   繁体   中英

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. Hence I have 2 questions but I guess strictly related one to another:


1) Access to the SinglePDO methods from a Manager object in the main code

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:

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? (I have tried: class Manager extends SinglePDO and using: __call() but w/o success)


2) Split SinglePDO into 2 classes
The SinglePDO class embeds lots of methods. 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...

Question:
How to achieve this in the previous context (still having the Manager Class)


Here is the SinglePDO class, a typical one but with additional methods.

       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

For the first question, did you use call in conjunction with 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. 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.

Answer to question #2 is to extend the classes one after another.

class Tools extends SinglePDO {}

class Manager extends Tools {}

BTW: weird implementation of a singleton pattern in PHP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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