<?php
class Demo{
private $where = ' 1=1 ';
public function getWhere()
{
echo $this->where;
}
public function When(string $whereStr = null, \Closure $callable):self
{
if ($whereStr && is_callable($callable)) {
$callable($this);
}
return $this;
}
public function where(string $where){
$this->where .= " and ".$where;
}
}
$a = 1;
$demo = new Demo();
$demo->When($a === 1, function ($cls){
$cls->where("id=2");
$cls->where("age >= 23");
});
$demo->getWhere();
thinkphp6 中 when 的实现原理
最新推荐文章于 2024-05-02 21:57:09 发布