PHP Polymorphism


The word 'Polymorphism' originated from the Greek word. Poly means "Many" and morphism means "Form". Polymorphism means Many Forms.

Single action is performed by different ways i.e., known as the polymorphism.

There are 4 pillars in oops. They are an inheritance, polymorphism, abstraction, and encapsulation.

php Polymorphism

Binding

The process of combining method with the object is known as Binding.


Early Binding

The Method overloading is the compile-time polymorphism which is known as the Static binding or Early binding. At the compiled time the type of object is determined, it is known as static binding.


Late Binding

The method overriding is the run-time polymorphism which is known as the Late binding or Dynamic binding. At the run-time the type of object is determined, it is known as Dynamic binding.


Example:

<?php
// Base class
class A {
    public function showdata() {
        echo "Base class\n";
    }
}

// Derived class
class B extends A {
    public function showdata() {
        echo "Derived class\n";
    }
}

// Main logic
$b = new B();
$b->showdata(); // Calls the overridden method in class B
?> 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.