A smart factory class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kawsper
Forum Newbie
Posts: 11
Joined: Sun Jul 10, 2005 12:21 pm

A smart factory class

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply