arborint is correct.
Polymorphism in PHP is how he demonstrated. And yes, PHP is actually easier to comprehend because there is *nothing* to understand it's all handled for you automagically.
Consider the example:
Code: Select all
class Instrument{
function play(){ echo 'Instrument::Play()'; }
}
class Wind extends Instrument{
function play(){ echo 'Wind::Play()'; }
}
class Percussion extends Instrument{
function play(){ echo 'Percussion::Play()'; }
}
function tune(Instrument $i)
{
$i.play();
}
tune(new Percussion()); // Play a percussion
tune(new Wind()); // Play a wind
Notice in the function
tune() the argument type is of the base class
"Instrument". This is essential because you want the function to be able to accept *any* derived type of Instrument. If it was of type
"Percussion" the function
tune() would only be able to play percussion instruments which is NOT polymorphic. The historical problem with the aboce code is that, the methods of that type of object would be called, not the intended class method (of the derived classes). This is the problem polymorphism was introduced to solve.
The above code, when compiled in a language such as C++ the output is not what you would expect.
Output from above code:
Code: Select all
Instrument::Play()
Instrument::Play()
Because of
early binding. You usually need to explicitly indicate which functions are "virtual" via a simple function modifier and this makes that function or the object "polymorphic".
It is the late, dynamic or runtime binding of a function call that *is* the essence of polymorphism in OOP. PHP Does all this for you, so how did this disscussion even start?
Cheers
