rt
abstract class Node {
private $debugMessages;
public function __construct() {
$this->debugMessages = array();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
$this->dumpDebug();
}
protected function debug($msg) {
$this->debugMessages[] = $msg;
}
private function dumpDebug() {
echo implode('<br />', $this->debugMessages);
}
public abstract function getView();
}
class ForumTopic extends Node {
private $debugMessages;
public function __construct() {
parent::__construct();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
parent::__destruct();
}
public function getView() {
return 'This is a view into '.__CLASS__.'<br />';
}
}
$forum = new ForumTopic();
echo $forum->getView();
执行结果:
This is a view into ForumTopic
Node constructor called.
ForumTopic constructor called.
ForumTopic destructor called.
Node destructor called.但没有new ForumTopic()怎么能够调用执行getView()?phpdestructorconstructorfunctionclass
abstract class Node {
private $debugMessages;
public function __construct() {
$this->debugMessages = array();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
$this->dumpDebug();
}
protected function debug($msg) {
$this->debugMessages[] = $msg;
}
private function dumpDebug() {
echo implode('<br />', $this->debugMessages);
}
public abstract function getView();
}
class ForumTopic extends Node {
private $debugMessages;
public function __construct() {
parent::__construct();
$this->debug(__CLASS__.' constructor called.');
}
public function __destruct() {
$this->debug(__CLASS__.' destructor called.');
parent::__destruct();
}
public function getView() {
return 'This is a view into '.__CLASS__.'<br />';
}
}
$forum = new ForumTopic();
echo $forum->getView();
执行结果:
This is a view into ForumTopic
Node constructor called.
ForumTopic constructor called.
ForumTopic destructor called.
Node destructor called.但没有new ForumTopic()怎么能够调用执行getView()?phpdestructorconstructorfunctionclass
解决方案 »
- php的mail()函数能够发送邮件吗?
- 出现乱码
- SoapClient 调用失败问题
- php写法的问题
- ftp上传问题
- 打开一个到doc东txt文件的链接时,想先判断该文件是否在(只要文件名,不在扩展名),如果存在的话直接打开,没有的话提示上载文件,怎么实现这
- 急急,phpmyadmin出现下面错误The configuration file now needs a secret passphrase (blowfish_secret).
- uploadify图片上传前 如何在onDialogClose中获取上传的文件名字??
- 求推荐网站代码编辑器
- 支付宝 异步回调验签
- 关于mysql,ajax与Jquery,php的编程,求大神帮忙
- 求PHP解析XML文件的代码。 100分求。谢谢。
ForumTopic::__construct()
Node::__construct()
Node::debug()
ForumTopic::debug()
ForumTopic::getView()
ForumTopic::__destruct()
ForumTopic::debug()
Node::__destruct()
Node::debug()
Node::dumpDebug()
你是先执行echo $forum->getView();所以先输出:This is a view into ForumTopic
在对象的销毁的时候,父类析构函数把$debugMessages中的所有数据打印出来了。就下面那样:
Node constructor called.
ForumTopic constructor called.
ForumTopic destructor called.
Node destructor called.
__CLASS__指是的当前类get_class($obj)指的是实例$obj的类
protected function debug($msg) {
$this->debugMessages[] = $msg;
}
改成
protected function debug($msg) {
echo $msg;
}
你就可以看到真正的执行顺序
LZ的意思是不是不要创建实例,直接调用getView方法。如果是就用类名::方法名。
ForumTopic::getView()