Jump to content

Sub classes and calling constructor from static method


shadowayex

Recommended Posts

This is going to be a sort of intriguing question, and I'll have to explain a little background so you get what I'm doing.I have a generic class with a private constructor and a static get method that is used to pass back an instance. With this, I have a few classes that extend this functionality. They, too, have private constructors that call the parent constructor, and then do some other things. So it looks something like this:

class SuperClass{	private function __construct()	{		// Do general stuff	}	public static function get()	{		// Some fancy stuff that results in an instance being returned	}}

class SubClass extends SuperClass{	private function __construct()	{		parent::__construct();		// Some more construction	}}

What I want to do is be able to call SubClass::get() (which would call the SuperClass get), but I want get() to call the constructor of the class calling get.The reason I have this set up is because I have many object being generated, and at time there could be many duplicates. So, what I've done is created a static array of objects, so get will check to see if the object attempting to be created has already been made once, and grabs that if it has. Otherwise, it calls the constructor. The constructors does some complex SQL stuff to pull data from a database and does some more fancy work to set the object properties. I decided this type of set up would help increase performance, due to the scale and high probability of duplicate objects being requested.Anyway, since I have many sub classes, I want to be able to have the SuperClass::get() method to call the constructor of the specific sub-class calling it. Can it be done?

Link to comment
Share on other sites

Why not inherit from the super class and override a parent property there? A call to SubClass::get() will then get an instance of that class, generated from SuperClass.

Link to comment
Share on other sites

Why not inherit from the super class and override a parent property there? A call to SubClass::get() will then get an instance of that class, generated from SuperClass.
I'm not quite sure I understand you.What my sub-classes do is uses all of the stuff provided by the super-class, plus adds some different things, depending on which sub-class is being used.If what you've suggested works for that, could you elaborate and/or provide an example so I can better understand your suggestion?Thanks =)
Link to comment
Share on other sites

Ok, either I'm not getting something, or my design is unclear.First, in my original post, SubClass should have an extends SuperClass on the class declaration. I somehow forgot it. I'll edit it after I'm done with this post.The get() function, aside from which constructor is called, is exactly the same for all sub-classes. That's why I wanted the super class to hold it. It literally looks like this:This is in SuperClass

public static function get($id){	if (!isset($objects[$id]))	{		$objects[$id] = /* Create an instance of whichever sub class called it */;	}	return $object[$id];}

What I'm having problems with is what to replace /* Create an instance of whichever sub class called it */ with. I tried self::__construct($id), but constructors can't be called statically. I don't want to do new SuperClass(), because each sub class has their own constructor that builds on SuperClass's construction. From there, I don't know what to do. I googled, but I didn't get anything with my terms, and I don't know how phrase my question in a way where google could find the information I desire.If it can't be done this way, then I need to know so I don't keep trying to make this way work.

Link to comment
Share on other sites

I got it figured out. The constructors needed changed to protected instead of private, and then I changed the get() function to this:

public static function get($id){	$class = get_called_class();	if (!isset(self::$objects[$id]))	{		self::$objects[$id] = new $class($id);	}	return self::$objects[$id];}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...