Polymorphism in PHP

Polymorphism sounds difficult in terms of its vocabulary but it’s not that complex. Today I will write some explanations along with simple code examples. There are two types of polymorphism called dynamic polymorphism and static polymorphism.

Let’s take a look at the dynamic polymorphism first. It can be achieved using interface.

There is an interface called Country and two of the concrete classes called Myanmar and Japan implemented it. The type of two concrete classes, Myanmar & Japan are same as they are the country but the function called talk inside their classes will be different depending on the country.

As I said earlier before their talk function is different. That is called dynamic polymorphism. Concrete classes are sharing the same interface as they are common in type but different in functionalities.

There is one more polymorphism called static polymorphism. This can be achieved using method overloading. We will use __call magic method as we are writing in PHP.

There are different return depending on the method arguments. The class talk can be adaptable depending on the function arguments which is called static polymorphism.

You can see the magic method __call reference here.


Thanks for reading. By Yuuma.




tel. 06-6454-8833(平日 10:00~17:00)