【发布时间】:2016-04-21 22:49:06
【问题描述】:
如何设计一个可以影响类的调用方法并从中获取输出的模式,例如当我们使用return $this 时,我们可以使用指针不断调用多个方法,但是在这种方法中,我们不能影响前面的方法通过最后调用的方法调用方法..
实例代码:
layout1.php
|--------------|
<div>
{content}
</div>
index.php
|-----------|
//return output
$view->layout('layout1')->content('test');
万一实例代码改为如下
//echo output
$view->layout('layout1');
我需要通知该函数并在发送输出时显示不同的行为。例如,在第一个代码中应该返回输出值,但在第二个代码中,必须直接回显该值..
示例代码:
layout1.php
|--------------|
<div>
{content}
</div>
index.php
|-----------|
$view = view();
//return output
$view->layout('layout1')->content('test');
//echo output
$view->layout('layout1');
view.php
|-----------|
class view{
public function layout($file){
return $this;
}
public function content($html){
return $this;
}
}
非常感谢您的帮助...
【问题讨论】:
标签: php oop design-patterns frameworks template-engine