If you can upgrade to PHP 5.3 ( released 30-Jun-2009 ), check out late static binding which could provide a solution: abstract class ParentObj { public function __construct(){} public static function factory(){ //use php5.3 late static binding features: $class=get_called_class(); return new $class; } } class ChildObj extends ParentObj{ function foo(){ echo " } } $child=ChildObj::factory(); $child->foo().
If you can upgrade to PHP 5.3 (released 30-Jun-2009), check out late static binding, which could provide a solution: abstract class ParentObj { public function __construct(){} public static function factory(){ //use php5.3 late static binding features: $class=get_called_class(); return new $class; } } class ChildObj extends ParentObj{ function foo(){ echo " } } $child=ChildObj::factory(); $child->foo().
I'm playing with RC4 at the minute so I'll try some code shortly if no one else steps up in the meantime... – Paul Dixon Jun 29 '09 at 19:58 LSB seems to be the best solution that I can find on my own. Until I upgrade, the best I can come up with is to either pass the child name to factory() or define the name as a property and grab that from factory() – Austin Hyde Jun 29 '09 at 20:00 According to the PHP manual, there is a function called get_called_class() that works like __CLASS__ us.php. Net/manual/en/function.
Get-called-class. Php – Austin Hyde Jun 29 '09 at 20:11 Ha! I didn't know about that, have amended the sample.
Thanks (I answer many questions in the hope of learning something! ) – Paul Dixon Jun 29 '09 at 20:23 Good way to learn. Someone famous once said that you don't truly learn something until you teach it.
– Austin Hyde Jun 29 '09 at 20:29.
In my humble opinion what you're trying to do makes no sense. A factory pattern would work like this: abstract class Human {} class Child extends Human {} class Fool extends Human {} class HumanFactory { public static function get($token) { switch ($token) { case 'Kid' : return new Child(); case 'King': return new Fool(); default : throw new Exception('Not supported'); } } } $child = HumanFactory::get('Kid').
Well, that's a full-blown factory pattern. I simply meant a static method to bypass the new operator and to provide readability and chaining to child classes. – Austin Hyde Jun 29 '09 at 20:09 why would you want to bypass the new operator?
– Philippe Gerber Jun 29 '09 at 20:10 to use your example, new Child()->foo() doesn't work. By adding the "factory" method (as I use it), you can do Child::factory()->foo()->bar(), which is more readable (IMHO) than breaking it up over a few lines. I guess it's a matter of opinion, and to be fair, your implementation of a factory is a useful one.
Just not for what I'm looking for. – Austin Hyde Jun 29 '09 at 20:27 1 you can use HumanFactory::get('Kid')->foo()->bar(). As foo() and bar() are methods inside the Human and/or Child class.
;-) – Philippe Gerber Jun 29 '09 at 21:05.
I need children to be able to call the factory function and return an instance of the calling class: $child = Child::factory(); and preferably without overriding the factory function in the child class. I have tried multiple different ways of achieving this to no avail. I would prefer to stay away from solutions that use reflection, like __CLASS__.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.