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

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

Dec 29, 2020 am 11:31 AM
thinkphp

"

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

#Routing is explained in detail above, starting from application initialization and parsing until route scheduling is returned to route detection.

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

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

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

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

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

What is returned after the route detection is executed isthink\route\dispatch\Module Object This class, and this class is assigned to the variable$dispatch

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

Next, take a look at the code of this method. Middleware is used here. 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

The place circled in the picture above is the code $dispatch->run(), and the next step is It’s time 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 (the routing address given above is used as an example).

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

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

Explain the abstract class

  • Abstract classes cannot be instantiated
  • A class with an abstract method must be an abstract class; the class must be decorated with abstract
  • Abstract method There cannot be a function body; that is, abstract function fun();
  • Non-abstract methods in abstract classes can be called by subclasses
  • Non-abstract The subclass inherits the abstract class, and the subclass must implement all the abstract methods of the parent class
  • Abstract subclasses inherit the abstract class, and there is no need to inherit 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

How to find out who has inherited Dispatch

Is there one at this time? One question is 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

Come tothinkphp/library/think/route/dispatch/Module.phpViewexecmethod.

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

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

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

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

So the parameters of this block will be passed into the hook and will be returned The hook instance, how this instance is returned is explained in detail in the container section, you can take a look!

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

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

At this time, you can come to the application behavior extension definition file, and you can see that this parameter is the module initialization , 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

Then the code will be executed until$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

And then through $obj = Container::get($class); Return an instance of behavior\LoadBehavior

Finally passed the is_callable function to verify whether the method in the class can be called, the method array format, this method will be used to write a separate article as an object to parse, here you only need to know It will return false.

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.

Instancing the 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 function will return an array, and then 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.

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

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

Then when I come to the controller to use it, I instantiate it first and implement the case before 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

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

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

Summary

In the implementation process of the first case, a step was ignored, which is to use objects to directly use arrays Form directly accesses the object's properties.

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

Let’s first take a look at the value of this app and print it out as think\App Object object.

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

When think\App Object this object accesses request, because the app attribute does not have thisrequest, 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

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

If you still have questions at this time, why does it just say that it will be executed? It will be implemented!

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

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

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

The use of ArrayAccess ends here. This is also explained in detail based on the previous one. Next, the __get method in the container will be explained in detail to see under what circumstances __get will be executed. method.

__Detailed explanation of how to use get method

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

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

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

You should be able to access attributes in the class, just use $this-> That’s it.

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

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

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

Summary

So use ArrayAccess and The __get magic method is ultimately executed and the 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'], execute ArrayAccess and then execute offsetGet

  • #__get is for the attributes of the class. When the attributes of the class do not exist, it will be executed.
  • ArrayAccess is used to access the instantiated class in the form of an array. When, if it does not exist, the offsetGet method will be executed.
    Recommended tutorial: "thinkphp"

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles