search

Home  >  Q&A  >  body text

What is the difference between PHP class declaration and instantiation?

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 memory
Class1 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!

習慣沉默習慣沉默2780 days ago494

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:12:23

    PHP doesn’t have this kind of syntax
    What you see should be something like this
    Route::get('/', function (IlluminateHttpRequest $request) {

    return view('welcome', ['a'=>$request->get('a',1)]);

    });
    This is dependency injection
    http://www.golaravel.com/lara...

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:12:23

    The following is wrong: After carefully looking at the source code, is the answer like this?
    at

    Request $request;
    $request->get();
    

    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:

     public static function getLoader()
        {
            if (null !== self::$loader) {
                return self::$loader;
            }
    
            spl_autoload_register(array('ComposerAutoloaderInit67db7509c61e60a4f92e012c704d3566', 'loadClassLoader'), true, true);
            self::$loader = $loader = new \Composer\Autoload\ClassLoader();
            spl_autoload_unregister(array('ComposerAutoloaderInit67db7509c61e60a4f92e012c704d3566', 'loadClassLoader'));
    
            $map = require __DIR__ . '/autoload_namespaces.php';
            foreach ($map as $namespace => $path) {
                $loader->set($namespace, $path);
            }
    
            $map = require __DIR__ . '/autoload_psr4.php';
            foreach ($map as $namespace => $path) {
                $loader->setPsr4($namespace, $path);
            }
    
            $classMap = require __DIR__ . '/autoload_classmap.php';
            if ($classMap) {
                $loader->addClassMap($classMap);
            }
    
            $loader->register(true);
    
            $includeFiles = require __DIR__ . '/autoload_files.php';
            foreach ($includeFiles as $fileIdentifier => $file) {
                composerRequire67db7509c61e60a4f92e012c704d3566($fileIdentifier, $file);
            }
    
            return $loader;
        }
    }
    

    It can be seen that if you use an uninstantiated class, it will be automatically loaded and instantiated.

    reply
    0
  • Cancelreply