Questions about Laravel controller modifiers

Why can the Laravel controller be accessed properly using protected decorations?

protected function advert()
        {
            try {
                $result = $this->systemService->advert ();
                
                return $this->response->array (Response::return (200, "", $result));
            } catch (\Exception $e) {
                return $this->response->array (Response::return (0, $e->getMessage ()));
            }
        }

even if you use reflection instantiation, you can"t access the protected-decorated method. The laravel source code is as follows

$constructor = $reflector->getConstructor();
        // If there are no constructors, that means there are no dependencies then
        // we can just resolve the instances of the objects right away, without
        // resolving any other types or dependencies out of these containers.
        if (is_null($constructor)) {
    
            array_pop($this->buildStack);
    
            return new $concrete;
        }

        $dependencies = $constructor->getParameters();
        // Once we have all the constructor"s parameters we can create each of the
        // dependency instances and then use the reflection instances to make a
        // new instance of this class, injecting the created dependencies in.
        $instances = $this->resolveDependencies(
            $dependencies
        );

        array_pop($this->buildStack);
        
        return $reflector->newInstanceArgs($instances);
Jul.20,2021

Illuminate\ Routing\ Controller this is how laravel calls the controller method

public function dispatch(Route $route, $controller, $method)
{
    
    $parameters = $this->resolveClassMethodDependencies(
        $route->parametersWithoutNulls(), $controller, $method
    );

    if (method_exists($controller, 'callAction')) {

            return $controller->callAction($method, $parameters);
    }
        
    return $controller->{$method}(...array_values($parameters));
}

Laravel invokes the specified method of the subclass, which is the custom method we want to call, through the callAction inherited by controller.

public function callAction($method, $parameters)
{
    return call_user_func_array([$this, $method], $parameters);
}

because it inherits from the parent class, it is natural that the parent class can call the protected methods of the child class.

Menu