func_get_arg OOP problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

func_get_arg OOP problem

Post by []InTeR[] »

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.

Code: Select all

class book()
{
  function book(&$dbObj,$priKey)
  {
    $this->dbObj = $dbObj;
    $author = new Author($this->dbObj,$this->get('author_id'));
  }
}
The new way (not working)

Code: Select all

class book()
{
  function book(&$dbObj,$priKey)
  {
    $this->dbObj = $dbObj;
    $author = singleton('Author',&$this->dbObj,$this->get('author_id'));
  }
}
The problem is, the func_get_arg can't handle reference, and will make a copy of the dbObj.

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; 
	}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

does this not work:

Code: Select all

$a =& func_get_arg(0);
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

You would think it would. But nope.

The function doesn't return the original object, but a copy.

The modification made in the dbObj called true the newly made Author are made in a copy. I can't see anything in the original class.


A solution would be, to get teh dbObj true the singleton as well. But that means day's work of modifications to the classes.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

The bug is know by php as a 'Feature/Change Request'.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

I have a workarround for this moment, but it's a nasty one.
Feel free to comment, if you have a better solution:

As i have a new problem:
I can't use this function for all the singleton needs, only for the specific db classes now.

And, even $a1 = func_get_arg(1); din't work here, as the bug-page suggested it would when you add the &$dbObj to your function params.

Code: Select all

function &singleton($className,&$dbObj) 
	{ 
		static $obj;
		global $config;
		
		if(!isset($obj[$className])){ 
			
			if(!class_exists($className)){
				//var_dump(get_included_files());
				include_once($config['dbclass']['path'].$className.$config['dbclass']['ext']);
			}
			
			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 = &$dbObj;
					$a1->querys[] = '// Does it work?';
					$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; 
	}
Post Reply