我有一个如下课程:
class page{
public $head_html = array();
///...........
public function head_html($input = ''){
if(isset($input) && !in_array($input,$this->head_html))
return $this->head_html[] = $input;
return implode("\n",$this->head_html);
}
///...........
}
$page = new page();
它已被用于许多页面,我不想进行影响其他页面的更改,因此我无法使类静态,...
但我想在我的页面中修改全局对象实例:
$page->head_html("css code");
$page->head_html("js code");
并像这样恢复:
echo $page->head_html();
但它没有显示任何内容......
当我尝试这个时:
echo implode("\n",$page->head_html);
它工作正常,但在某些情况下,获得价值会更复杂。 如何使用该类中的函数/方法实现此目的,而不更改已使用此类的其他页面和文件的行为?
答案 0 :(得分:1)
如果未在数组中传递$this->head_html[] = $input;
值,则返回head_html()
的真/假结果。那是你要的吗?如果您希望head_html()
返回更新后的值,请移除
return
return $this->head_html[] = $input;