Home >Backend Development >PHP Tutorial >一个对象如何能new不成功

一个对象如何能new不成功

WBOY
WBOYOriginal
2016-06-23 13:31:02903browse

类似这样:

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


回复讨论(解决方案)

为什么要这样?还是要单例模式?

把$t对象销毁了,就没有引用了

私有化构造函数

单例模式试试呗

找到了,直接在里边trigger_error了

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