Page 1 of 1

A smart factory class

Posted: Sun Aug 27, 2006 3:45 pm
by kawsper
Smart factory class. I want to build a smart user class based on the factory model. But i have some troubles grasping it, basicly i want to do something like this:

Code: Select all

class bla {
	function bla()
	{
		$this->lol();
	}
	function ()
	{
		echo "Yup!";
	}
	function ly()
	{
		echo "Yip!";
	}
}
class userFactory {
	private $userID;
	
	function userFactory($userID)
	{
		$waa = new bla();
		return $waa;
	}
}
try {
	$user = new userFactory(1);
	$user->lolly(); //Error
} catch (Exception $e) {
	echo $e->getMessage();
}
The class will then return different classes according to the users permissions and functions and such. Is it possible to do something like this?
Thanks.

Posted: Sun Aug 27, 2006 3:51 pm
by feyd
The constructor of a class is unable to "return." You will either need a method to call in your factory or return the newly created object via a reference you pass into the constructor.

The recommended route is the former over the latter.