Call private method from inherited class

泄露秘密 提交于 2019-12-07 04:53:20

问题


I want to implement a hook-system in my simple ORM, in PHP:

class Record {
  public function save() {
    if (method_exists($this,"before_save")) {
      $this->before_save();
    }
    //...Storing record etc.
  }
}

class Payment extends Record {
  private function before_save() {
    $this->payed_at = time();
  }
}

$payment = new Payment();
$payment->save();

This results in a Fatal Error:

Fatal error: Call to private method Payment::before_save() from context 'Record' in

Makes sense.

I could change the scope to public, but that seems ugly: no-one but Payment has anything to do with before_save(). It is best left private, IMHO.

How can I make Record call a private method on the class inheriting from the Record?


回答1:


Add a dummy before_save function to your Record class, set its accessibly to protected. Now all classes that inherit from Record will have this function, if they don't overwrite it it will do NOTHING. If they overwrite it, it can implement the desired functionality.

class Record {
  public function save() {
    $this->before_save();
    //...Storing record etc.
  }

  protected function before_save() {
     return;
  }
}

class Payment extends Record {
  protected function before_save() {
    $this->payed_at = time();
  }
}



回答2:


Check the error message

Call to private method Payment::before_save() from context 'Record'

This means that you are trying to call a function defined in Payment while you are within Record. Class Record does not have a before_save method because it is further up in the inheritance chain than where the function is defined.

In other words since, the parent-child relation is Record (is parent of) Payment, Payment has access to Records functions (by virtue of inheriting from the parent) but not vice-versa (parent cannot "inherit" child class functions). You can make your function protected which will give it access up and down the inheritance chain, but you might want to rethink the architecture and decide if you want it so.. Ideally you should have the function defined in Record and have it overridden in Payment

Also (and I might be wrong with this), but checking explicitly for method_exists is usually not required unless you are creating a really dynamic system where run time classes can be overlapped and/or generated. If you are defining a class based system from ground-up and you know how you are stitching up the various pieces, usually you would not need to check during run-time if method_exists...just a thought..




回答3:


change the scope to protected:

http://php.net/manual/en/language.oop5.visibility.php




回答4:


Visibility and the rules of inheritance in PHP:

Members declared protected can be accessed only within the class itself and by inherited and parent classes




回答5:


class test{

private function pri($val){
    return $val;
}

function caller(){
   return $this->pri(5);
}

}

$testobj = new test; echo $testobj->caller();

You will get 5 as output.

By this way You can access the private function of a class.



来源:https://stackoverflow.com/questions/12603766/call-private-method-from-inherited-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!