Question

Suppose I have this classes

<?php
class Grandparent {
    public function myMethod($param) {
        //do something
    }
}
class Parent extends Grandparent {
    public function myMethod($param) {
        //do something
        return parent::myMethod($param) ;
    }
}
class Son extents Parent {
    public function myMethod($param) {
        //do something
        //code to access parent parent
    }
}
?>

I want in myMethod of Son class to avoid call his parent and access to this grandparent. The code I tried was parent::parent::myMethod($param) but and the Ide throws me an error. Which is the way to access in a static way to grandparent with out passing through parent?

Is there any posibilities? I know there are some methods like get_parent_class, but I dont want them to use it. I need to call in static way.

Was it helpful?

Solution

http://www.php.net/manual/ru/language.oop5.inheritance.php

<?php
class foo
{
  public function something()
  {
    echo __CLASS__; // foo
    var_dump($this);
  }
}

class foo_bar extends foo
{
  public function something()
  {
    echo __CLASS__; // foo_bar
    var_dump($this);
  }
}

class foo_bar_baz extends foo_bar
{
  public function something()
  {
    echo __CLASS__; // foo_bar_baz
    var_dump($this);
  }

  public function call()
  {
    echo self::something(); // self
    echo parent::something(); // parent
    echo foo::something(); // grandparent
  }
}

error_reporting(-1);

$obj = new foo_bar_baz();
$obj->call();

// Output similar to:
// foo_bar_baz
// object(foo_bar_baz)[1]
// foo_bar
// object(foo_bar_baz)[1]
// foo
// object(foo_bar_baz)[1]

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top