Jump to content

Recommended Posts

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.