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:
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: