脚本宝典收集整理的这篇文章主要介绍了php – “Class X扩展Y(抽象),Y实现Z(接口). “不能调用接口Z的抽象方法”,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
接口类(最顶层的抽象)的要点是强制那些较低的实现具有自己的静态公共函数id($params = false){方法.
// My top level abstraction,to be implemented only by "MyAbstraction" interface MyInterface{ static public function id(); }
// My second (lower) level of abstraction,to be extended // by all child classes. This is an abstraction of just the // common heavy lifting LOGic,common methods and PRoPErties. // This class is never instantiated,hence the "abstract" modifier. // Also,this class doesn't override the id() method. IT is left // for the descendant classes to do. abstract class MyAbstraction implements MyInterface{ // Some heavy lifting here,including common methods,properties,etc // .... // .... static public function run(){ $this->id = self::id(); // This is failing with Fatal error } }
// This is one of many "children" that only extend the needed methods/properties class MyImplementation extends MyAbstraction{ // As you can see,I have implemented the "forced" // method,coming From the top most interface abstraction static public function id(){ return 'XXX'; } }
最终的结果是,如果我打电话:
$o = new MyImplementation(); $o->run();
interface MyInterface{ public function id(); } abstract class MyAbstraction implements MyInterface{ public $id; // Some heavy lifting here,etc // .... // .... public function run(){ $this->id = static::id(); // This is failing with Fatal error } } class MyImplementation extends MyAbstraction{ // As you can see,I have implemented the "forced" // method,coming from the top most interface abstraction public function id(){ return 'XXX'; } } $o = new MyImplementation(); $o->run();
在上面的代码中,static :: id()将调用上下文中的类函数,即MyImplementation类.
这种现象称为Late Static Binding
以上是脚本宝典为你收集整理的php – “Class X扩展Y(抽象),Y实现Z(接口). “不能调用接口Z的抽象方法”全部内容,希望文章能够帮你解决php – “Class X扩展Y(抽象),Y实现Z(接口). “不能调用接口Z的抽象方法”所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。