leela Posted July 1, 2011 Share Posted July 1, 2011 Hai all, i have an abstract class , from there i extended the child class, i extended further and made it as final..and i was trying to overload parent methods ..but i cannot overload parent methods using _call..pleadse can anyone help me out.. here is my code : abstract class Shape { protected $length; protected $height; protected $a; protected $b; protected $c; public function getCoordinates($length,$height) { $this->length=$length; $this->height=$height; } public function getSides($a,$b,$c) { $this->a=$a; $this->b=$b; $this->c=$c; } abstract public function area(); abstract public function perimeter(); abstract public function display(); } class rectangle extends Shape { public function area() { return round(($this->length)*($this->height),2); } public function perimeter() { return round(2*(($this->a)+($this->b)),2); } public function display() { echo "area is :". rectangle::area() . "<br>"; echo "perimeter is : ". rectangle::perimeter() ."<br>"; } } final class triangle extends rectangle { function __call($method_name, $arguments) // this is wrong ........please modify here to call area(),which is in shape class(); { $accepted_methods = array("getCoordinates","area","perimeter"); } // this is wrong, i know......i want to overload area() in parent class to overload ..how ? public function area() { return round((($this->length)*($this->height)*($this->width)/2),2); } public function perimeter() { return round((($this->a)+($this->b)+($this->c)),2); } public function getCoordinates($length,$height,$width) { $this->length=$length; $this->height=$height; $this->width=$width; } public function display() { echo "area is :". triangle::area() . "<br>"; echo "perimeter is : ". triangle::perimeter() ."<br>"; } } $r=new rectangle(); $r->getCoordinates(1,2,4); $r->getSides(6,2); $r->display(); $ot = new triangle(); $ot->getCoordinates(1,2,4); $ot->getSides(6,2,3); $ot->display(); thanks in advance ... Link to comment https://forums.phpfreaks.com/topic/240842-overloading-parent-methods/ Share on other sites More sharing options...
mikesta707 Posted July 1, 2011 Share Posted July 1, 2011 You can call a parent classes method from a base class by using the scope resolution operator in combination with the parent keyword. using your example class triangle extends rectangle { ... public function area(){ //i can call parent by doing this parent::area(); //i should probably return the value returned by parent though return parent::area(); } ... }//end class EDIT: actually that would probably call Rectangle's area() method since rectangle is the parent of triangle, not the base class they are both derived from. Let me do a little research and get back to you. The basic idea described above is solid Edit2: After a bit of research, I am unsure if you can access things at the parent's parent's class scope without some ugly hackish type code. One suggestion would be to add a boolean parameter to rectangle's area method which indicates that it would call the parent method instead of doing it normally. for example class rectangle extends Shape { public function area($callBase = false) { if ($callBase) return parent::area(); return round(($this->length)*($this->height),2); } public function perimeter() { return round(2*(($this->a)+($this->b)),2); } public function display() { echo "area is :". rectangle::area() . "<br>"; echo "perimeter is : ". rectangle::perimeter() ."<br>"; } } final class triangle extends rectangle { function __call($method_name, $arguments) { $accepted_methods = array("getCoordinates","perimeter"); } public function area() { return parent::area(true); } ... } ... ... While something like this may work, it may be beneficial to review your class structure. Perhaps triangle should inherit from shape. A triangle isn't really a rectangle. Something like Square would be a bitter fit for a child of rectangle. Its about the number of sides, and making triangle a child of rectangle is like making rectangle a child of a 5 or more sided shape, like a pentagon or hexagon. The areas/circumferences/volume/etc. are all calculated differently for shapes of different number of sides. Link to comment https://forums.phpfreaks.com/topic/240842-overloading-parent-methods/#findComment-1237078 Share on other sites More sharing options...
KevinM1 Posted July 1, 2011 Share Posted July 1, 2011 In the future, please post code in either or tags. I took the liberty of doing it for you this time. Link to comment https://forums.phpfreaks.com/topic/240842-overloading-parent-methods/#findComment-1237079 Share on other sites More sharing options...
leela Posted July 1, 2011 Author Share Posted July 1, 2011 Thanku so much......next time, if i post code,definitely i would post code in or [code=php:0] Link to comment https://forums.phpfreaks.com/topic/240842-overloading-parent-methods/#findComment-1237180 Share on other sites More sharing options...
leela Posted July 1, 2011 Author Share Posted July 1, 2011 hi, here i didnot find any use of _call ? what is the use.......can i use base class area () for child class triangle and rectangle too ...... please tell me Link to comment https://forums.phpfreaks.com/topic/240842-overloading-parent-methods/#findComment-1237190 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.