Home > DeveloperSection > Forums > How to implement overloading in PHP?
Allen Scott

Total Post:36

Points:254
Posted on    June-23-2016 3:59 AM

 PHP PHP 
Ratings:


 1 Reply(s)
 206  View(s)
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


Ailsa Singh
Ailsa Singh

Total Post:9

Points:63
Posted on    June-23-2016 4:05 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick