jeudi 4 avril 2019

Method Overloading in Laravel is not working

I am trying to overload my method in php which is working pretty fine. But when I am trying to do the same in one of my laravel project, then i can not see the output from that overloaded function.

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);

but when I am trying to do the same like the following, I am not getting any output ::

In my 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;
    }

Now I am calling that method from another class ::

public function results(Request $request){
        $params = $request->all();
        unset($params['_token']);
        $absModel = new AbstractAPIModel('ssbsect');
        $absModel->executeRestAPI("GET", $params);

Here, I am not getting any output. Even I tried to print '12' to check whether this function is invoking or not. But i did not find any success.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire