HOW TO IMPLEMENT OVERLOADING IN PHP?

Allen Scott

Total Post:46

Points:324
Posted by  Allen Scott
PHP 
PHP
 505  View(s)
Ratings:
Rate this:
Hi Guys 

I am beginner in php language.I want to know that how to implement overloading in php. Please tell me with example

Thanx
  1. Ailsa Singh

    Post:24

    Points:168
    Re: How to implement overloading in PHP?

    Hi Allen following is an example of overloading with the help of magic methods:


    <?php

    class TDshape {

    const Pi = 3.142 ;  // constant value

     function __call($fname, $argument){

        if($name == 'area')

            switch(count($argument)){

                case 0 : return 0 ;

                case 1 : return self::Pi * $argument[0] ; // 3.14 * 5

                case 2 : return $argument[0] * $argument[1];  // 5 * 10

            }

        }

    }

    $circle = new TDshape();

    echo "Area of circle:".$circle->area(5)."</br>"; // display the area of circle

     $rect = new TDshape();

    echo "Area of rectangle:".$rect->area(5,10); // display area of rectangle

    ?>

      Modified On Jun-23-2016 04:20:12 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!