#php #laravel-5
#php #laravel-5
Вопрос:
Я пытаюсь перегрузить свой метод в php, который работает довольно нормально. Но когда я пытаюсь сделать то же самое в одном из моих проектов laravel, я не вижу выходных данных этой перегруженной функции.
class MyClass {
public function __call($name, $args) {
switch ($name) {
case 'funcOne':
switch (count($args)) {
case 1:
return call_user_func_array(array($this, 'funcOneWithOneArg'), $args);
case 3:
return call_user_func_array(array($this, 'funcOneWithThreeArgs'), $args);
}
case 'anotherFunc':
switch (count($args)) {
case 0:
return $this->anotherFuncWithNoArgs();
case 5:
return call_user_func_array(array($this, 'anotherFuncWithMoreArgs'), $args);
}
}
}
protected function funcOneWithOneArg($a) {
print($a);
}
protected function funcOneWithThreeArgs($a, $b, $c) {
print $a." ".$b." ".$c;
}
protected function anotherFuncWithNoArgs() {
print "Nothing";
}
protected function anotherFuncWithMoreArgs($a, $b, $c, $d, $e) {
print $a." ".$b." ".$c." ".$d." ".$e;
}
}
$s = new MyClass;
$s->anotherFunc(1, 2, 3, 4, 5);
но когда я пытаюсь сделать то же самое, как показано ниже, я не получаю никаких выходных данных ::
В моем APIModel.php ::
public function __call($functionName, $arguments)
{
// TODO: Implement __call() method.
switch ($functionName){
case 'executeRestAPI':
switch (count($arguments)){
case 3:
dd("aa");
return call_user_func(array($this, 'executeRestAPIWithThree'), $arguments);
case 4:
return call_user_func(array($this, 'executeRestAPIWithFour'), $arguments);
}
}
}
protected function executeRestAPIWithThree($methodType, $request = array(), $referenceID = null){
dd(12);
if (isset($referenceID))
$this->apiEntity = $this->apiEntity."/".$referenceID;
$response = $this->httpClient->request($methodType,$this->apiEntity, ['query' => $request]);
dd($response);
return $response;
}
Теперь я вызываю этот метод из другого класса ::
public function results(Request $request){
$params = $request->all();
unset($params['_token']);
$absModel = new AbstractAPIModel('ssbsect');
$absModel->executeRestAPI("GET", $params);
Здесь я не получаю никаких выходных данных. Даже я пытался напечатать ’12’, чтобы проверить, вызывается ли эта функция или нет. Но я не нашел никакого успеха.
Комментарии:
1. Из какого класса вы расширили свой
AbstractAPIModel
?
Ответ №1:
$absModel->executeRestAPI("GET", $params);
Вы передаете 2 аргумента, таким образом, ваш случай переключения никогда не будет соответствовать чему-либо…
Попробуйте:
call_user_func_array([$absModel, 'executeRestAPI'], array_merge(['GET'], $params));