如何在php中更改类方法行为和输出
我如何设计一种模式来影响类的调用方法并从中获取输出,例如,当我们使用如何在php中更改类方法行为和输出,php,oop,design-patterns,frameworks,template-engine,Php,Oop,Design Patterns,Frameworks,Template Engine,我如何设计一种模式来影响类的调用方法并从中获取输出,例如,当我们使用return$this时,我们可以使用指针不断调用多个方法,但在这种方法中,我们不能通过最后调用的方法影响之前调用的方法 实例代码: layout1.php |--------------| <div> {content} </div> index.php |-----------| //return output $view->layout('layout1')->conten
return$this
时,我们可以使用指针不断调用多个方法,但在这种方法中,我们不能通过最后调用的方法影响之前调用的方法
实例代码:
layout1.php
|--------------|
<div>
{content}
</div>
index.php
|-----------|
//return output
$view->layout('layout1')->content('test');
我需要通知函数,并在发送输出时显示不同的行为。例如,在第一个代码中,应返回输出值,但在第二个代码中,必须直接回显值
示例代码:
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;
}
}
layout1.php
|--------------|
{content}
index.php
|-----------|
$view=view();
//返回输出
$view->layout('layout1')->content('test');
//回波输出
$view->layout('layout1');
view.php
|-----------|
类视图{
公共功能布局($file){
退还$this;
}
公共函数内容($html){
退还$this;
}
}
非常感谢您的帮助…您好,实际上不可能更改链调用规则。 你能做的就是改变方法
$view->content('test')->layout('layout1');
问题解决后:)
或
及
}如何返回$view->layout('layout1');到变量?
$view->content('test')->layout('layout1');
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