Home > PHP Framework > ThinkPHP > body text

ThinkPHP about the difference between ArrayAccess and direct magic access return instance

咔咔
Release: 2020-12-04 13:32:23
Original
136 people have browsed it
"

This article will instantiate the controller as a primer and then analyze the difference between ArrayAccess and directly executing magic access to return the instance

"

Preface

#In the above section, routing is explained in detail, starting from application initialization to route scheduling. Return to the routing detection link.

The value obtained by route detection is as shown below, which is the value finally returned by route scheduling.

The routing rule used is Route::get('hello/:name', 'index/index/:name');

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Printing place
ThinkPHP about the difference between ArrayAccess and direct magic access return instance
The result of routing detection printing

As can be seen from the above figure, important data are stored in dispatchc, then The controller will be explained in detail below.

The first thing to explain is the instantiation controller operation performed after the routing detection is completed.

1. Instantiation Controller

Let’s first take a look at how to execute the instantiation controller!

There is no doubt that the code must be executed first from the entry file. Here, the container is used to return an instance of App, and then the run method in the App class is called.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Entry file

When you come down, you will come to the execution application. In this method, it is also the route just parsed above.

So after the detection route is executed, the instantiated controller will be executed.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Execute the application

After the route detection is executed, the class think\route\dispatch\Module Object is returned. And this class is assigned to the variable $dispatch

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Route detection

Then let’s take a look at the code of this method. Here we use What is middleware is that closures are still used in this quick code. If you are not clear about the concept of closures, you need to go back to the basics.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Middleware

The place circled in the picture above is the code $dispatch->run(). Next, we need to analyze this piece of code.

After detecting the final return value of the route, we can know that this method is actually in the think\route\dispatch\Module class.

Then you need to analyze the run method in this class, which is to perform routing scheduling.

In this method, neither obtaining routing parameters nor detecting routing, nor automatic data verification will be executed (this is based on the routing address given above by Kaka as an example).

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Execute routing scheduling

So according to the above code, the code will be executed to$data = $this->exec();here.

Tracing this method will lead to the existence of an abstract class in the picture below. What you need to know here is the abstract class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Abstract class

Explain the abstract class

  • Abstract classes cannot be instantiated
  • A class with abstract methods must be an abstract class; the class must be decorated with abstract
  • Abstract methods cannot have function bodies; that is, abstract function fun();
  • Non-abstract methods in abstract classes can be called by subclasses
  • Non-abstract subclasses inherit abstract classes, and subclasses must implement all abstract methods of the parent class
  • Abstract subclasses inherit abstract classes without inheriting the abstract methods of the parent class

According to the principle of the above figure, you can see that DispatchThis class is an abstract class.

So there will be two situations. One is that the abstract class inherits the abstract class without inheriting the abstract method of the parent class.

The other is that non-abstract subclasses inherit abstract classes, and the subclass must implement all abstract methods of the parent class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Abstract class

How to find who has inherited Dispatch

Do you have a question at this time? How to find the subclass of Dispatch.

You can see this type of Dispatch in this picture, but there is also a dispatch directory.

According to the data returned by route detection, you can easily know that it is the class thinkphp/library/think/route/dispatch/Module.php.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Looking for subclasses

Come tothinkphp/library/think/route/dispatch/Module.phpViewexec method.

Then the next task is to conduct an in-depth interpretation of this method.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Instantiate the controller

Look at the first line of code first$this->app['hook']->listen( 'module_init');, here the container ArrayAccess is used to access the object in the form of an array, and then execute the magic method __get. When accessing a non-existent attribute, the make method will be executed.

Use the editor to track this app and you will go to thinkphp/library/think/route/Dispatch.phpThis class. In the constructor of this class, you can see that the attribute for app is An App instance is assigned.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Look for the instantiation of the app class

Then when you come to the App class, you can see that it is inherited from the Container class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Inherited class

I have talked about this knowledge point more than once in the context of containers. To access non-existent properties, go back and execute the __get magic method of the container. .

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
The magic method will be executed when accessing non-existing properties

So the parameters of this block will be passed into the hook, and the instance of the hook will be returned. About this How the instance is returned is explained in detail in the container section, you can check it out!

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Return the instance of the hook

Then the listen method of the hook will be executed to monitor the behavior of the tag.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Monitoring label behavior

At this time, you can come to the application behavior extension definition file. You can see that this parameter is initialized for the module, but because this value is empty.

So it will not be executed in the above picture, so put the application initialization value into this parameter for a simple test.

This class is the execution hook, the optimization operation of the facade class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Application behavior extension definition file

Then the code will be executed to$results[$key] = $this->execTag($name, $ tag, $params);Here.

Parameter description

  • $name = string(22) "behavior\LoadBehavior"
  • $tag = module_init

Then the passed parameters are processed through regular expressions, and finally moduleInit is returned

Then pass $obj = Container::get($class); Return the instance of behavior\LoadBehavior

Finally, verify through the function is_callable to detect whether the method in the class can be called, the method array format, this method will be written in a separate article later To parse it as an object, you only need to know that false will be returned.

Then the $portal value of this class will be assigned to $method, and this value is run.

Finally pass$result = $this->app->invoke($call, [$params]);This line of code, the bottom execution of this line of code is through reflection implemented by the mechanism.

The last code will return NULL.

Instance Controller

The next step is to instantiate the controller. The calling method is $this->app->controller()

What needs to be noted here is the list function, this An array will be returned at the end of the function, and the two variables in the list will be indexes 0 and 1 respectively.

The judgment will also execute the first one, and it will also execute the make method of the container class. This method will directly return the instance of app\index\controller\Index.

实例化(分层)控制器 格式:[模块名/]控制器名
Instantiation (layered) controller format: [module name/] controller name

2. Regarding the difference between ArrayAccess and direct execution of magic access to return instances

Some friends have learned the use of ArrayAccess and the magic method __get.

It is estimated that some of them are in ambiguous areas in these two places. Kaka will put these two together and analyze them once.

Let’s talk about the use of ArrayAccess first

This case has been demonstrated to you before, mainly to implement the ArrayAccess class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
This is a test method written before

Then when you come to the controller for use, instantiate it first. The previously implemented case is as follows.

But the case that needs to be implemented this time is not what is implemented in the figure below.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Using the controller

Next, use the method shown in the figure below to access the object properties directly using the array.

In the picture above, you can see that an attribute title is set to kaka. In this case, it can be obtained directly in the form of an array.

See that the return result is kaka, which means that the properties of the object are directly accessed in array form.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
New visit visit
ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Print results

Summary

In the implementation process of the first case, one step was ignored, which is to use the object to directly access the properties of the object in the form of an array.

What you can see can be obtained directly, so let’s put this idea into the framework and take a look.

Framework practical case

The following code exists in the routing parsed in the previous article. Let’s briefly analyze it.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Routing class

Let’s first take a look at the value of this app. The printed value is the think\App Object object.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Printed app value

When think\App Object this object accesses request, because The app attribute does not have this request, and because the app class inherits the container class, it will go to the container class to execute the method below.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Execute this method

Then the __get method will be executed, and the make method will be executed to return the corresponding instance.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance返回实例对象
Execute this method to return the instance object

If you still have questions at this time, why does it just say it will be executed and it will be executed!

Next, Kaka will take everyone to do a simple test and you will know.

Print a random value in this position.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Test Case

Then go to the offsetGet method of ArrayAccess of the container class and print the passed value.

Looking at the print results, it is very clear.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Insert picture description here
ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Print results

That’s it for the use of ArrayAccess That’s it. This is also a detailed explanation based on the previous one. Next, we will explain the __get method in the container in detail to see under what circumstances the __get method will be executed.

____get method usage detailed explanation

For this case, please see the $this->hook in the picture below.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Initializing the application

For the same reason, let’s first debug the value of $this.

There is no need to print this value, because it is in this class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Print results

The attributes in the class should be accessible, just use $this-> directly.

So when the system accesses $this->hook, since the App class does not have the hook attribute, it will execute the magic method of the container class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Magic method

Then execute the make method to create an instance of the class.

ThinkPHP about the difference between ArrayAccess and direct magic access return instance
Creating instances of classes

Summary

So we use ArrayAccess and __get magic methods, and ultimately the executed make method returns an instance of the class.

When encountering this->config is the __get method of the container that is executed.

When encountering app['request'], ArrayAccess is executed and then offsetGet is executed.

  • __get is for the attributes of the class. It will be executed when the attributes of the class do not exist.
  • ArrayAccess When accessed in the form of an array using an instantiated class, if it does not exist, the offsetGet method will be executed.

The above is the detailed content of ThinkPHP about the difference between ArrayAccess and direct magic access return instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!