Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Sep 27 2008

Polymorphism in PHP

Published by at 3:15 am under Patterns,PHP

In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B. In non-strongly typed languages (dynamically typed languages) types are implicitly polymorphic to the extent they have similar features (fields, methods, operators). In fact, this is one of the principal benefits of dynamic typing. – Wikipedia
Polymorphism is tightly coupled to the inheritance and is often considered to be one of the most powerful feature of the object oriented programming. It can be defined as a term according to which a name (variable declaration) may denote objects of many different classes that are related by some common suprclass; thus, any object denoted by this name is able to respond to some common set of operations in different ways. – Grady Booch

Lets consider an example:
There are two array of shapes called Rectangle and Circle. Even though we treat both these as a Shape but their implementation is quite different. In nutshell, each class is able to respond differently to the same method getArea(). This is called Polymorphism.

UML Diagram

PHP Code


4 responses so far

4 Responses to “Polymorphism in PHP”

  1. lead net pro scamon 01 Feb 2011 at 2:01 pm

    This suggests that the MTHFR 677 C>T polymorphism played a minimal role or no role in stroke risk. However, the data suggest that the MTHFR TT genotype may …

  2. Prefab Homeon 17 Feb 2011 at 4:51 am

    Very Nice website. I just finished mine and i was looking for some design ideas and your website gave me some.

  3. Lawyer in Miamion 25 Feb 2011 at 9:04 am

    Synthesio, on the other hand, added consulting services on top of its initial monitoring platform after about a year, a decision that has further differentiated the brand from competitors like Radian 6. Explained Moisand, “we really want to nurture our

  4. trenbolone acetateon 27 Feb 2011 at 6:58 am

    The 5-HTT genotype and rs1042173 polymorphism interacted significantly. LL/TT individuals treated with ondansetron had the lowest number of drinks and highest number of days of abstinence compared with all other genotype and treatment groups. …

RSS Feed
Subscribe by email
Follow me @ Twitter