|
Posted by Jerry Stuckle on 06/25/06 13:32
Tim Van Wassenhove wrote:
> On 2006-06-25, David Haynes <david.haynes2@sympatico.ca> wrote:
>
>>PHP5 has weak polymorphism but not true polymorphism in the sense that
>>it is used in OOP.
>
>
> So how would you define polymorphism? And what exactly are the
> differences between 'weak' and 'true' polymorphism?
>
> If i look at http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29
>
> "The concept of polymorphism applies to data types in addition to
> functions. A function that can evaluate to and be applied to values of
> different types is known as a polymorphic function. A data type that
> contains elements of different types is known as a polymorphic data
> type."
>
>
>>function __construct($one, $two="two", $three="three") {
>>
>>I cannot instantiate this object as new Foo($one, $three) since there is
>>no typing on the arguments and, therefore, no signature for a 'one,
>>three' contructor.
>
>
> Imho that's the same as saying: I'm standing with my back against a wall,
> and now i'm wondering why i can't step backwards anymore...
>
> Define your constructor as __constructor($args) and handle with
> func_num_args and func_get_args(s) any number of parameters...
>
> (I do agree that the language/compiler can, probably should, make this
> easier... But that's a different discussion.)
>
The constructor overloading example isn't really polymorphism. It's just
function overloading.
Polymorphism is the ability to operate on objects of derived classes, without
knowing what the derived classes are (or potentially even the existence of the
derived classes).
For instance - let's say we have class "mammal" with function "eats". Now all
mammals eat something, but what the class of mammals eats is not defines.
So, derive from mammal the class "ape" and have eats return "bananas". Also
derive the class "horse" and have eats return "oats".
Now - when you create an object of the class "ape" you can pass it to a function
which takes a "mammal". And you can print out what *this* mammal eats. The
same with a horse.
Polymorphism is closely tied to inheritance. But while inheritance allows the
programmer to take advantage of the commonalities between classes, polymorphism
allows the program to take advantage of the differences.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|