我想通过类内部的方法将数组推送到请求数组方法。 所以我这样写:
class Check {
protected $request;
function __construct($request) {
$this->request = $request;
}
public function author($request) {
$this->request->merge(['author' => 1 ]);
$this->identifier($this->request);
}
public function identifier($request) {
$this->request->merge(['identifier' => 'test' ]);
$this->kip($this->request);
}
public function kip($request) {
$this->request->merge(['kip' => 1 ]);
$this->NextMethodName($this->request);
}
// many more methods here that should merge to $request variable
}
有没有一种干净的方法来防止重复$ this-> NextMethodName($ this-> request)吗?
答案 0 :(得分:2)
好吧,有一种棘手的方法可以做到这样
function __construct($request) {
$this->request = $request;
// get class methods
$methods = get_class_methods($this);
// remove constructor and static call method
$methods = array_diff($methods, [ '__construct', 'create_save_request']);
// execute every methods for $request to prepare save request array
foreach ($methods as $method) call_user_func([$this, $method]);
}