A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearIs what seems like polymorphism in PHP really polymorphism? - page 1
User InfoPosts
Is what seems like polymorphism in PHP really polymorphism?#1
Trying to figure out whether PHP supports features like method overloading, inheritance, and polymorphism, I found out:


it does not support method overloading
it does support inheritance


but I am unsure about polymorphism. I found this Googling the Internet:


I should note that in PHP the
polymorphism isn(t quite the way it
should be. I mean that it does work,
but since we have a weak datatype, its
not correct.


So is it really polymorphism?

Edit
Just can(t quite place a definite YES or NO next to PHP supports polymorphism. I would be loath to state: "PHP does not support polymorphism", when in reality it does. Or vice-versa.

posted date: 2009-04-14 15:30:00


Re: Is what seems like polymorphism in PHP really polymorphism?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-14 15:30:01


Re: Is what seems like polymorphism in PHP really polymorphism?#3
You can still override methods, just not overload them. Overloading (in C++) is where you use the same method name for multiple methods, differing only in number and types of parameters. This would be hard in PHP since it(s weak-typed.Overriding is where the sub-class replaces a method in the base class. Which is really the basis for polymorphism, and you can do that in PHP.

posted date: 2009-04-14 15:33:00


Re: Is what seems like polymorphism in PHP really polymorphism?#4
class Animal { var $name; function __construct($name) { $this->name = $name; }}class Dog extends Animal { function speak() { return "Woof, woof!"; }}class Cat extends Animal { function speak() { return "Meow..."; }}$animals = array(new Dog((Skip(), new Cat((Snowball());foreach($animals as $animal) { print $animal->name . " says: " . $animal->speak() . (<br>(;}You can label it all you want, but that looks like polymorphism to me.

posted date: 2009-04-14 15:38:00


Re: Is what seems like polymorphism in PHP really polymorphism?#5
__call() and __callStatic() should support method overloading. More on this is available in the manual. Or what exactly are you after?UPDATE: I just noticed the other replies.For another way to overload a method, consider the following:<?phppublic function foo(){ $args = func_get_arg();}Certainly not pretty, but it allows you to do virtually whatever you want.

posted date: 2009-04-14 15:39:00


Re: Is what seems like polymorphism in PHP really polymorphism?#6
sorry, this is not exactly what I meant. I understand why method overloading is not supported in PHP, and the difference between overloading and overriding. But can I proclaim it does support polymorphism?

posted date: 2009-04-14 15:40:00


Re: Is what seems like polymorphism in PHP really polymorphism?#7
sure, but the Animal interface does not specify there will be a speak() method. So I could construct an object like new FPDF(); and then add it to the array of animals. What then? How would it speak() ?

posted date: 2009-04-14 15:43:00


Re: Is what seems like polymorphism in PHP really polymorphism?#8
You can specify a speak method in the Animal class, and it would be called if the child class does not specify its own speak. And if it does, you can call it anyways with parent::speak();

posted date: 2009-04-14 15:47:00


Re: Is what seems like polymorphism in PHP really polymorphism?#9
this is a nice hack :) good to keep in "tule box" :)

posted date: 2009-04-14 15:52:00


Re: Is what seems like polymorphism in PHP really polymorphism?#10
So you would say it DOES support polymorphism. I am thinking of calling it 'unsafe' or 'weak polymorphic behaviour'. what's your opinion?

posted date: 2009-04-14 15:53:00


Re: Is what seems like polymorphism in PHP really polymorphism?#11
tool box? :) Hehe... yeah, just remember, to not use it. Also, in case you are after monkey patching, look into runkit.

posted date: 2009-04-14 15:59:00


Re: Is what seems like polymorphism in PHP really polymorphism?#12
unsafe and weak aren't words I'd use, to be honest. PHP does also support method overloading as Till shows, but it's not nearly as clean as it should be. I'd say it has polymorphism, all things considered.

posted date: 2009-04-14 16:03:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.