Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

inheritance - How do I call PHP parent methods from within an inherited method?

In PHP, I'm trying to reference a method defined in an object's parent class, from a method inherited from the object's parent class. Here's the code:

class base_class {
  function do_something() {
    print "base_class::do_something()
";
  }
  function inherit_this() {
    parent::do_something();
  }
}
class middle_class extends base_class {
  function do_something() {
    print "middle_class::do_something()
";
  }
}
class top_class extends middle_class {
  function do_something() {
    print "top_class::do_something()
";
    $this->inherit_this();
  }
}

$obj = new top_class;
$obj->do_something();

The problem is that parent::do_something() in inherit_this() tries to find the parent class of base_class, not the parent of the object's actual class, and the example above throws an error. Is there something I can write instead of parent::do_something() that would call middle_class::do_something(), and that would still work even in classes that extend (say) top_class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get it work you can modify your base_class like this:

class base_class {
  function do_something() {
    print "base_class::do_something()
";
  }

 function inherit_this() {
    $this->do_something();
 }
}

Then your top_clas will call inherit_this() of your base class, but there will be a recursion: do_something() of top_class calls $this->inherit_this(), and in base_class you call again $this->do_something() (in your base class $this will reference to your top_class). Because of that, you will call inherit_this() over and over again.

You should rename the methods to prevent that.

Update

If you want that base_class inherit_this() prints "base_class::do_something" you could modify your base_class like this:

class base_class {
  function do_something() {
    print "base_class::do_something()
";
  }

  function inherit_this() {
     base_class::do_something();
  }

}

In this case you make a static call to the base_class method do_something(). The output is top_class::do_something() base_class::do_something()

Update 2

Regarding to your comment you can modify your base_class like this:

class base_class {
  function do_something() {
    print "base_class::do_something()
";
  }
  function inherit_this() {    
    $par = get_parent_class($this);
    $par::do_something();
  }
}

You get the parrent class of $this and then call the method. Output will be: top_class::do_something() middle_class::do_something()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...