Problem: we have an hierarchy of 3 classes, each extending the one in front (grandfather, father, son). A method – let’s say foo() – is defined in the grandfather class and overridden with a new functionality in the father class.

Question: Is there a way in the son class to access the original method (with the grandfather code) in the son class?

Of course, the obvious solution is to try something like:

parent::parent::method();

But it won’t work. It will just yield an error like:

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in …

After some struggling, I have found a way to get around and access a method from the grandparent class that was overridden in the parent and run its “original” code. It’s loosely based on the strange PHP scoping that I wrote about in this article. The approach goes something like this:

class Grandfather
{
    protected $_message = 'Luke, I am your grandfather';

    public function say()
    {
        echo $this->_message;
    }
}

class Father extends Grandfather
{
    protected $_message = 'Luke, I am your father';   

    public function say()
    {
        throw new Exception("I can't breath under this f**cking mask :( ");
    }
}

class Son extends Father
{
    protected $_message = 'I have a very bad feeling about this';

    public function say()
    {
        Grandfather::say();
    }
}

$son = new Son();
$son->say();

Now PHP will bind the $this pointer in the body of the say() method – using the code defined in the grandfather. I guess that weird scoping is there for a reason.