What is "auto casting" and how to use it?

Asked

Viewed 50 times

0

I have the class y:

class y {
        private $group = [];
        public function __construct(){
            $this->group['Start'] = array(
                'GET' => array(
                    'Home' => array(
                        'Method' => 'ControllerHome@index',
                        'Logged' => true,
                        'Parameters' => false
                    )
                )
            );   
        }

        public function GET($g, $function, $logged, $parameters){
            $explode = explode('/', $g);
            if(array_key_exists($explode[0], $this->group)){
                if(!array_key_exists($explode[1],  $this->group[$explode[0]]['GET'])){
                    $this->group[$explode[0]]['GET'][$explode[1]] = array(
                        'Method' => $function,
                        'Logged' => $logged,
                        'Parameters' => $parameters
                    );
                    //var_dump($this->group);
                    $t = new z('a');
                    return $t;
                }
            } else {
                $this->group[$explode[0]]['GET'] = [];
                $this->GET($g, $function, $logged, $parameters);
            }
        }

        public function POST($g, $function, $logged, $parameters){
            $explode = explode('/', $g);
            if(array_key_exists($explode[0], $this->group)){
                if(!array_key_exists($explode[1],  $this->group[$explode[0]]['GET'])){
                    $this->group[$g]['GET'][$explode[1]] = array(
                        'Method' => $function,
                        'Logged' => $logged,
                        'Parameters' => $parameters
                    );
                    var_dump($this->group);
                }
            } else {
                $this->group[$explode[0]] = [];
                $this->x($g, $function, $logged, $parameters);
            }
        }
    }

Just below I have the class z:

class z {
        public function __construct($teste){

        }

        public function r($mensagem){
            return $mensagem;
        }
    }

What I want is to call the function of the other class - in case r().

$a = new y();
$a->GET('k/home', 'ControllerHome@index', true, false)->r('teste');

Is there a specific name for this action? How can I use it? Because I’m getting the error below?

Error: Call to a member function r() on null in C:\laragon\www\nota_veio_novo\index.php on line 70
  • In his job GET add the return: retur $this->GET($g, $function, $logged, $parameters);

  • William, thank you for answering, but it hasn’t worked yet..

  • I think you can work as follows:https://answall.com/questions/16769/usar-function

  • Thanks for answering Elton, I just wanted to understand how it works the way I showed on top... but I’ll take a look there, thanks!!

  • @Joaopedro, Probably the code is falling on else (of the method GET), returns nothing. By default, the PHP considers this value as null. That’s why the mistake.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.