[英]use Variable as methods in PHP chain to allow conditional method chaining
有条件建立链条的方法吗? 例:
$chain = $restaurant->allFoods()->$filter->get();
然后$filter
是动态的或有条件地设置
if ($user == "vegetarian")
{
$filter = "->onlyVegetables()";
}
因此,如果满足条件,则链条将变为:
$chain = $restaurant->allFoods()->onlyVegetables()->get();
其他
$chain = $restaurant->allFoods()->get();
这可能吗? 这个叫什么? 谢谢
是的,通过方法__get / __ call的重载,取决于是否需要将参数传递给过滤器。
<?php
function __get($user) {
if($user == 'vegetarian') {
return $this->onlyVegetables();
}
return $this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.