首页 / PHP / 详解PHP单例模式之继承碰见的问题
详解PHP单例模式之继承碰见的问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了详解PHP单例模式之继承碰见的问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1818字,纯文字阅读大概需要3分钟。
内容图文
![详解PHP单例模式之继承碰见的问题](/upload/InfoBanner/zyjiaocheng/147/def6c352d1354239970ab25d30647de3.jpg)
<?php // 单例模式之继承 class Singleton { protected static $ins = null; private final function construct() { } protected final function clone() { } // public static function getIns() { // if(self::$ins === null){ // self::$ins = new self(); // } // return self::$ins; // } public static function getIns() { if(static::$ins === null){ static::$ins = new static(); } return static::$ins; } } class Child extends Singleton { // protected static $ins = null; } /*输出结果为: bool(true) object(Singleton)#1 (0) { } 问题:对象 $c1, $c2 竟然都是 Singleton 的实例 ??? 解决方法:将 getIns() 方法中关键字 self 替换为 static, 利用后期静态绑定的特性 */ $c1 = Child::getIns(); $c2 = Child::getIns(); var_dump($c1 === $c2); //true var_dump($c1); // ------------------------------------------------------------------------ // 另一个问题 /* 输出结果为: bool(true) object(Child)#1 (0) { } 问题:对象 $c3 竟然是 Child 的实例, 实际上应该是 Singleton 的实例 ??? 原因:因为 $ins 属性是从父类 Singleton 继承过来的, 当第一次调用 Child::getIns() 时, $ins = new Child() 当再次调用 Singleton::getIns() 时, $ins 已经被实例过了, 而且指向 Child 的实例, 所以此时 $c3 变成了 Child 的实例 解决方法:在 Child 类中, 声明自己独有的 $ins 属性 */ $c3 = Singleton::getIns(); var_dump($c1 === $c3); var_dump($c3);
后期静态绑定的 getIns() 方法还会有一个问题:
若 Singleton 的 $ins 属性被设置为 private 的,子类 Child 必须设置自己的 $ins 属性,
因为 static::$ins 优先寻找子类自己的 $ins 属性,但由于子类没有声明且父类的不能继承,此时调用 Child::getIns()
方法便会报错:
Fatal error: Cannot access property Child::$ins in D:\wamp\www\mycode\DesignPattern\Singleton.php on line 27
解决方法:
将父类 Singleton 的 $ins 属性设置为 protected 的 或者 设置子类 Child 自己的 $ins 属性
以上就是详解PHP单例模式之继承碰见的问题的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的详解PHP单例模式之继承碰见的问题全部内容,希望文章能够帮你解决详解PHP单例模式之继承碰见的问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。