I am a newbie, and I found that many tool classes can be used by directly declaring them, such as those in laravel
Request $request;
$request->get();
This puzzles me. Does declaring a class variable instantiate the code?
After searching on Baidu, no one asked about this issue in PHP language, only Java, and Java is like this:Class1 item1;
Declare an object
Generate a reference type, Occupying 4 bytes of memory, no matter what type (object o or From f) the reference is, it occupies 4 bytes of memoryClass1 item1 = new Class1();
Create a class Instance, open a piece of memory (this memory is related to the size of the class itself), and let the reference item1 point to the starting position of this memory;
If so, $request
is just a reference to Request
, and Request
has not been instantiated yet and does not exist in memory. , then why can $request
directly use the methods in it? After looking at the source code, although the properties and methods in this class are static
static and already exist in the memory before instantiation, there are also many non-static properties and methods, get( ) is a non-static method!
Wouldn’t calling it directly cause a pointer exception?
Ask a newbie!
PHP doesn’t have this kind of syntax
What you see should be something like this
Route::get('/', function (IlluminateHttpRequest $request) {
});
This is dependency injection
http://www.golaravel.com/lara...
The following is wrong: After carefully looking at the source code, is the answer like this?
at
Before these two pieces of code, I used
use IlluminateHttpRequest;
use just uses the namespace, but if you want to call the class, you must load the class file (require), or load it automatically. , lavavel uses automatic loading, so in our opinion, using IlluminateHttpRequest is equivalent to being able to use the Request class directly. In fact, it is not. Laravel actually does a lot of work when using the Request class!
Laravel’s automatic loading function is as follows:
It can be seen that if you use an uninstantiated class, it will be automatically loaded and instantiated.