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

一个对象怎么能new不成功

WBOY
Release: 2016-06-13 12:22:46
Original
1514 people have browsed it

一个对象如何能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 />
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template