Grandparent method call

Probably I am missing something about the php workings but listen to this because it is working!

Let’s say we have three classes


class Grandparent {

    public function myMethod($param) {

        return 1;



class Parent extends Grandparent {

    public function myMethod($param) {

        return parent::myMethod($param) + 1;



class Son extents Parent {

    public function myMethod($param) {

        return parent::myMethod($param) + 1;




Let’s assume that you do not want to change the Parent or the Grandparent class because it is the source code developed from somebody else and you only have authorization to create the Son class.

Let’s also assume that there is some functionality inside the parent class which is unwanted !!

Moreover you want all the functionality that there is inside the Grandparent class !!!

How do you skip to call the parent method ? Let me remind you that the chain parent::parent::myMethod() is invalid !

Well you simply do this:


$grandparent = get_parent_class(get_parent_class($this));



get_parent_class only returns a string. So after two calls what you have inside $grandparent variable is a string ‘Grandparent’.

My first thought was that if I just call Grandparent::myMethod I would get an error because myMethod is NOT a static method and therefore cannot be called statically.

Well this is NOT the case here since this call works perfectly!

The grandparent method is called and we have successfully skipped calling parent method!

In case you know WHY this works, please share :)

Seems you have an older PHP version or you don’t have the E_NOTICE enabled…

On PHP 5.3.5 (probably from 5.3 and above) you cannot define a class “Parent” because it’s a reserved word…

[size=“2”]And if you call statically a method that it’s not you get an error like:


To make it work properly you need to instantiate the grandparent class like

$son = new Son();

$grandparent = get_parent_class(get_parent_class($son));

$gp= new $grandparent;

echo $gp->myMethod(1);


mdomba I currently have php 5.3.6 and did you really not get that "Parent" was only for the explanation?? :confused:

Of course you cannot define a class as parent, I named it this way merely for an example !! (obvious I believe)

I also have all errors on now in development, E_STRICT etc.

Yes that is what I am telling you. I do NOT get this error. This was not a question of mine. Did you even try my code to see for yourself that it works?

Anyway thanks for the interaction :)

Pligor dude: relax.

If I were you I’d change the example you’re using. It’s misleading.

Don’t attack Mdomba - he is one of the coolest features of Yii. Always extremely helpful.


As you put the code in a code block… it’s expected to be working with copy/paste… even if you use class/variable names for explanation… that’s why I wrote you about the “parent” reserved word…

Of course I tested this (PHP 5.3.5)… where would I get the error I posted above?

Note that this error is shown only if you enable E_NOTICE… and that one is not included in E_ALL.


Thank you :D

First of all I am relaxed don’t worry I know you are both most active members of this forum :)

So is it possible to call the grandparent method after all?

I also thought that E_STRICT and E_ALL covers all situations. How many more are there ?? I mean if you want all error messages displayed

// PHP < 5.4


// PHP >= 5.4