PHP-在非对象上调用方法,但它显然是一个对象
class DOM extends ContentTag {
private $body;
private $head;
public function Head() {
return $head;
}
public function Body() {
return $body;
}
public function __construct() {
parent::__construct('html');
Tag::Extras('xmlns="http://www.w3.org/1999/xhtml"');
$head = new ContentTag('head');
$body = new ContentTag('body');
ContentTag::AddTag($head);
ContentTag::AddTag($body);
$head->AddTag(MakeTag('meta')->Extras('http-equiv="Content-Type" content="text/html; charset=utf-8"'));
}
public function Emit() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
Emit();
}
}
// top.php
$pagediv = 0;
$view = new DOM();
$view->Head()->AddTag(MakeLink('css/style.css', 'stylesheet', 'text/css'));
这段代码在底行失败,访问变量然后调用 AddTag 失败 - 即使我在 DOM 构造函数中调用了该确切变量的 AddTag 也很好。代码解析得很好 - 这是一些奇怪的优先级..什么还是什么?
class DOM extends ContentTag {
private $body;
private $head;
public function Head() {
return $head;
}
public function Body() {
return $body;
}
public function __construct() {
parent::__construct('html');
Tag::Extras('xmlns="http://www.w3.org/1999/xhtml"');
$head = new ContentTag('head');
$body = new ContentTag('body');
ContentTag::AddTag($head);
ContentTag::AddTag($body);
$head->AddTag(MakeTag('meta')->Extras('http-equiv="Content-Type" content="text/html; charset=utf-8"'));
}
public function Emit() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
Emit();
}
}
// top.php
$pagediv = 0;
$view = new DOM();
$view->Head()->AddTag(MakeLink('css/style.css', 'stylesheet', 'text/css'));
This code fails on the bottom line, where accessing the variable and then calling AddTag fails- even though I called AddTag on that exact variable in the constructor of DOM just fine. The code parses fine- is this some strange precedence.. something or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
$this->head
设置并返回$head
属性。否则,
$head
属性将为NULL
。You need to set and return
$head
property with$this->head
.Otherwise the
$head
property will beNULL
.