3

I am trying to understand how to use class hierarchies in PHP. In Java programming i would be able to do something like this:

public class ClassOne extends SuperClass{

}

public class ClassTwo extends SuperClass{

}

public class SuperClass{

}

Now when i instantiate an object as unknownclass = new SuperClass(); I would then be able to cast this unknownclass to any of the classes it extends on the go.

My question is how does this apply to PHP ? and how do you take it in use?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • 1
    Eh, you cannot change the object itself. You can write `unknownclass = new ClassOne();`. But i think that is not your question, maybe you can clarify? – Haneev Dec 16 '13 at 18:13
  • Perhaps you might consider using interfaces instead. http://www.php.net/manual/en/language.oop5.interfaces.php For what it's worth, I submitted some code to the Daily WTF that I found awhile back where someone serialized an object, used regex to change its class name, and unserialized it as something else. It was a nightmare. – Brad Dec 16 '13 at 18:14
  • 2
    May be [this](http://stackoverflow.com/questions/8542661/general-polymorphism-with-php-examples) can help. – Ravindra S Dec 16 '13 at 18:14
  • this has **nothing** to do with polymorphism – hek2mgl Dec 16 '13 at 18:19
  • 1
    also note that PHP has traits, something which Java doesn't have, which makes classes not always the best solution. http://php.net/manual/en/language.oop5.traits.php – Mike 'Pomax' Kamermans Dec 16 '13 at 18:20

1 Answers1

0

The primary benefit of polymorphism is the ability to pass something specific for a generic requirement, provided that the specific thing responds to the generic requirement. So, given the following generic requirement:

public function doSomethingWith(Superclass obj)

you can pass an instance of Superclass or any of its child classes to doSomethingWith. But for this to be useful and "safe", the child classes of Superclass need to implement at least the same public members that Superclass does. And so if Superclass has a method called execute, ClassOne and ClassTwo need to have the same method, either by inheriting Superclass's implementation, or by overriding it and defining a fresh implementation, so long as the overridden method has the same (or polymorphically compatible) signature. This gives you a great deal of behavioral flexibility, because the differing implementations of the same generic requirement can produce variation.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
  • You misunderstood polymorphism – hek2mgl Dec 16 '13 at 18:25
  • @hek2mgl Would you care to tell me how? – Brian Warshaw Dec 16 '13 at 19:32
  • First, was AFK , sorry for the late response. I read some stuff about that topic it seems that I'm wrong with my comment, but you aren't complete with your answer. First, it would be nice if you could edit the post a little bit - just because then SO would allow me to remove the downvote, because it isn't wrong. I had just method based polymorphism in mind, meaning having the same method with a different argument set. However, this type shouldn't being forgotten when talking about polymorphism – hek2mgl Dec 16 '13 at 21:26