Home  >  Article  >  Backend Development  >  一个对象怎么能new不成功

一个对象怎么能new不成功

WBOY
WBOYOriginal
2016-06-13 12:22:461461browse

一个对象如何能new不成功
类似这样:

<br />class MyClass {<br />  private $index;<br />  public function __construct($index) {<br />    if ($index === 0) {<br />      // 销毁对象<br />    }<br />    $this->index = $index;<br />  }<br /><br />  public function getIndex() {<br />    return $this->index;<br />  }<br />}<br /><br />$t = new MyClass(1);<br />echo $t->getIndex(); // 正确返回<br /><br />$t = new MyCalss(0);<br />echo $t->getIndex(); // 失败。<br />

------解决思路----------------------
为什么要这样?还是要单例模式?
------解决思路----------------------
把$t对象销毁了,就没有引用了
------解决思路----------------------
私有化构造函数
------解决思路----------------------
单例模式试试呗

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn