func_get_arg OOP problem
Posted: Thu Nov 03, 2005 4:59 am
I'm needing a singleton, in a existing project.
The problem was, that in a list multiple of the same 'parent' classes was made.
Like, displaying a list of books, and for every book a new object was made for the same author.
So, i made a singleton, to solve that problem.... BUT. now the real problem occours, with $this->dbObj. it's a object that handles the connection and do the query's.
The old way, in the constructor of book.
The new way (not working)
The problem is, the func_get_arg can't handle reference, and will make a copy of the dbObj.
The problem was, that in a list multiple of the same 'parent' classes was made.
Like, displaying a list of books, and for every book a new object was made for the same author.
So, i made a singleton, to solve that problem.... BUT. now the real problem occours, with $this->dbObj. it's a object that handles the connection and do the query's.
The old way, in the constructor of book.
Code: Select all
class book()
{
function book(&$dbObj,$priKey)
{
$this->dbObj = $dbObj;
$author = new Author($this->dbObj,$this->get('author_id'));
}
}Code: Select all
class book()
{
function book(&$dbObj,$priKey)
{
$this->dbObj = $dbObj;
$author = singleton('Author',&$this->dbObj,$this->get('author_id'));
}
}Code: Select all
function &singleton($className)
{
static $obj;
if(!isset($obj[$className])){
switch(func_num_args()) {
case 1:
$obj[$className] = &new $className();
break;
case 2:
$a1 = func_get_arg(1);
$obj[$className] = &new $className($a1);
break;
case 3:
$a1 = func_get_arg(1);
$a2 = func_get_arg(2);
$obj[$className] = &new $className($a1,$a2);
break;
case 4:
$a1 = func_get_arg(1);
$a2 = func_get_arg(2);
$a3 = func_get_arg(3);
$obj[$className] = &new $className($a1,$a2,$a3);
break;
default:
die('Singleton error params');
break;
}
return $obj;
}
return $obj;
}