preface

Reflection classes in PHP are the opposite of instantiated objects. Instantiation calls methods and members of the enclosing class, while reflection unwraps all methods, member variables, and private methods. Just like the “solution plane,” we can call any keyword modified method, member. Of course not recommended in normal business, the comparison reflection class has abandoned the concept of encapsulation.

This chapter explains the use of reflection classes and Laravel’s use of reflection.

reflection

ReflectionClass is an internal PHP class that can be used without loading. You can use it by instantiating ReflectionClass.

methods

Here are some common PHP reflection class methods

The method name annotation
ReflectionClass::getConstant Gets a constant defined
ReflectionClass::getConstants Gets a set of constants
ReflectionClass::getConstructor Gets the constructor of the class
ReflectionClass::getDefaultProperties Get default properties
ReflectionClass::getDocComment Get document comments
ReflectionClass::getEndLine Gets the number of rows in the last row
ReflectionClass::getFileName Gets the name of the file that defines the class
ReflectionClass::getInterfaceNames Gets the name of the interface
ReflectionClass::getMethods Gets an array of methods
ReflectionClass::getModifiers Gets the modifier for the class
ReflectionClass::getName Get the name of the class
ReflectionClass::getNamespaceName Gets the name of the namespace
ReflectionClass::getParentClass Access to the parent class

Wait, wait, wait…. All of the methods, attributes, their inherited parents, and implemented interfaces of the class can be queried. For detailed documentation, please refer to php.net/manual/zh/c…

chestnuts

<? php namespace A\B; class Foo { } $function = new \ReflectionClass('stdClass'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName()); $function = new \ReflectionClass('A\\B\\Foo'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName()); ? >Copy the code

The output

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"
Copy the code

Laravel

Laravel uses reflection classes when implementing service container loading. Now let’s go into “Unplane” mode

Entrance to the file

index.php

$app = require_once __DIR__.'/.. /bootstrap/app.php'; /* |-------------------------------------------------------------------------- | Run The Application |-------------------------------------------------------------------------- | | Once we have the application, we can handle the incoming request | through the kernel, and send the associated response back to | the client's browser allowing them to enjoy the creative | and wonderful application we have prepared for them. | */ $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); $response->send(); $kernel->terminate($request, $response);Copy the code

The make method is called on the next line where the reference statement occurs. As you are well aware, the make method is used to parse classes, and any implementation of the make method must be in the referenced file.

bootstrap\app.php

$app = new Illuminate\Foundation\Application( realpath(__DIR__.'/.. / '));Copy the code

Laravel starts loading its core classes, and all implementations start with Illuminate\Foundation\Application.

Illuminate\Foundation\Application

public function make($abstract, array $parameters = [])
{
        $abstract = $this->getAlias($abstract);

        if (isset($this->deferredServices[$abstract]) && ! isset($this->instances[$abstract])) {
            $this->loadDeferredProvider($abstract);
        }

        return parent::make($abstract, $parameters);
}

Copy the code

In the core class you’ll probably find exactly the make method, which loads the service provider and then calls the parent make method. Remember that as a separate module “service container” is never written in the core class. Anyone who knows anything about design patterns knows that.

Illuminate\Container\Container

For $API = $this – > app – > make (‘ HelpSpot \ API, [‘ id ‘= > 1]). For example

// The real make method, // $abstract = 'HelpSpot 'public function make($abstract, 'HelpSpot') array $parameters = []) { // $abstract = 'HelpSpot\API' return $this->resolve($abstract, $parameters); }... protected function resolve($abstract, $parameters = []) { ... $this->isBuildable($this->isBuildable); $object = $this->build($concrete); $this->build($concrete); } else { $object = $this->make($concrete); }... } public function build($concrete) { // $concrete = 'HelpSpot\API' if ($concrete instanceof Closure) { return $concrete($this, $this->getLastParameterOverride()); $reflector = new ReflectionClass($concrete); // Check if the class can be instantiated if (! $reflector->isInstantiable()) { return $this->notInstantiable($concrete); } $this->buildStack[] = $concrete; $constructor = $reflector->getConstructor(); if (is_null($constructor)) { array_pop($this->buildStack); return new $concrete; } $dependencies = $constructor->getParameters(); $instances = $this->resolveDependencies( $dependencies ); array_pop($this->buildStack); // Create a new class instance from the parameters given. return $reflector->newInstanceArgs($instances); }Copy the code

See that a service container is loaded successfully.

Thank you

Thank you for reading here, this article source code parsing depends on personal understanding. If there is any discrepancy, please clap the brick.

I hope this article helped you. thank you

communication

There is life, there is code.

Spread the positive energy of technology and continue to learn new knowledge.