如何设计一个可以影响类的调用方法并从中获取输出的模式,例如当我们使用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;
}
}
非常感谢您的帮助......
答案 0 :(得分:0)
嗨实际上不可能改变连锁电话规则。 你可以做的是改变方法
$view->content('test')->layout('layout1');
当你的问题解决了:)
OR
class view{
public function layout($file){
return $this;
}
public function content($html){
return $this;
}
public function __toString(){
return $vars; // in a string format
}
和
echo $view->layout('layout1'); // will echo the result
$view->layout('layout1')->content('test'); // return result
}