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 ?>
Liked By
Write Answer
How to implement overloading in PHP?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Join MindStick Community
You have need login or register for voting of answers or question.
Ailsa Singh
23-Jun-2016Hi Allen following is an example of overloading with the help of magic methods: