Перегрузка метода в Laravel не работает - PullRequest
0 голосов
/ 04 апреля 2019

Я пытаюсь перегрузить мой метод в 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 Ответ

0 голосов
/ 04 апреля 2019
$absModel->executeRestAPI("GET", $params);

Вы передаете 2 аргумента, поэтому ваш регистр переключателя никогда не будет ничего соответствовать ...

Попробуйте:

call_user_func_array([$absModel, 'executeRestAPI'], array_merge(['GET'], $params));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...