PHP Overloading


Method Overloading

Two or more methods have the same name but different parameters are called Method overloading.

Syntax:

 class A 
{   
		void show(int a)
        {
         //body of the method. 
        }
        void show(int a, int b)
        {
         //body of the method. 
        } 
}
PHP Function Overloading

Example:

 <?php
class A {
    public function show() {
        $args = func_get_args();
        $count = func_num_args();

        if ($count == 1) {
            echo "Area of square: " . ($args[0] * $args[0]) . "\n";
        } elseif ($count == 2) {
            echo "Area of Rectangle: " . ($args[0] * $args[1]) . "\n";
        } else {
            echo "Invalid number of arguments\n";
        }
    }
}

$a = new A();
$a->show(5);
$a->show(5, 7);
?>



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.