0

Is it possible in PHP that a object transforms itself into one of his children class.

example

class Children1 extends Parent {
    // the children
}

class Parent {
    public function loadChildConfiguration($type){
        select($type){
            case 1: self::MAGICALLY_TRANSFORM_INTO(Children1); break;
            case 2: self::MAGICALLY_TRANSFORM_INTO(Children2); break;
            // etc...
        }
    }
}

$foo = new Parent();    // foo is a Parent
$foo->loadChildConfiguration(1);    // foo is now a Children1

For now the only idea I had was to create a static class in the parent as new constructor

class Parent {
    public static constructByType($type){
        select($type){
            case 1: $class = Children1; break;
            case 2: $class = Children2; break;
            // etc...
        }
        return new $class;
    }
}

$bar = Parent::constructByType(1);    // bar is a Children1

This should work as far as I do not need to get the children class after the parent creation.

Is there a way to change an object to its child after it has been created? Maybe there is another "cleaner" way to load new methods and parameter to an existing object?

benomite
  • 848
  • 7
  • 23
  • 3
    I suggest you to look for "Factory pattern" in Google – STT LCU Jul 04 '13 at 13:36
  • @STTLCU Exactly what I was about to answer. Factory, or maybe Builder (which is similar) are the common OO patterns for this. 'Magically extending an object with extra methods' is more the Javascript way to go. – GolezTrol Jul 04 '13 at 13:38
  • If you're crazy you can do this: http://stackoverflow.com/a/1147414/2200766 – chrislondon Jul 04 '13 at 13:38
  • @STTLCU OP already uses _factory pattern_ in the second example, I assume, (s)he already knows what it is. – Leri Jul 04 '13 at 13:39
  • This is a possible duplicate of http://thedailywtf.com/Articles/Pot-o-Gold.aspx – Kris Jul 04 '13 at 13:41
  • @PLB despite the second option written by the OP, i dare to say that (s)he does not know that what's written is quite similar to a pattern. I still stand by my ground. – STT LCU Jul 04 '13 at 13:42
  • 2
    @PLB A parent class doesn't normally know of its children. Therefore, the parent class itself shouldn't be the factory. A separate factory class, unrelated to the class hierarchy of the class being instantiated, should create the right child class. – GolezTrol Jul 04 '13 at 13:43
  • @PLB You're right, I didn't know about the Factory pattern, I'm currently having a look at this. – benomite Jul 04 '13 at 13:48

1 Answers1

1

What you're actually looking for is something that abides to the so called "Factory Pattern"

You can find more details HERE

Basically you define a "factory" object which has the task to build the right type of object based on the specified needs (usually, you decide which object to build by calling a different method). Please note this is an overly broad semplification, i really suggest you to read the resource i linked you.

Also, you may want to take a look of another similar creational pattern, the "Builder Pattern" which, as the name suggests, is tasked with the creation of new objects.

More details HERE

PLEASE NOTE: Once you discover the design patterns, don't be struck in awe. They're merely tools, not the be-all and end-all of software developing. Basically, do USE design patterns, do not ABUSE (or over-use) design patterns! We have all been there and done that. Try not to follow us :D

STT LCU
  • 4,348
  • 4
  • 29
  • 47