The namespace

  • A namespace is a virtual directory for a file. If you do not have a namespace, the code of multiple files will be merged into one file, and the method of repeating the name will cause an error

PHP Fatal error: Cannot redeclare test() (previously declared in /Users/zhangguofu/website/default/IMooc/Test/Test1.php:9) in /Users/zhangguofu/website/default/IMooc/Test/Test2.php on line 12

Test1 has declared the test method. Test2 cannot declare it

image.png

We added the namespace to the test function to see what happened, and found that the execution was successful, and each call to test was preceded by its own namespace. That’s what namespaces are for

  • But we also found that every time we wanted to use a method in a file, we had to require it once, which was very cumbersome, so we invented a function__autoloadWhen a class is being used but not loaded, this function will try to load the class file, for example.
  • We use the Autoload function (deprecated after 7.2) toload the class

image.png

  • I’ll do it in PHP5.6
Guofu @ guofu: / home/below $/ usr/local/default/Example/php5.6 demo_class/bin/PHP. PHP Test \ Test1 \ T_Class PHP Fatal error: Class 'Test\Test1\T_Class' not found in /home/wwwroot/default/Example/demo_class.php on line 9Copy the code
  • It is important to note that the namespace must be consistent with the directory and the class name must be consistent with the file name so that it can be mapped to related files, which is also the specification of PSR-0
  • The namespace of the PSR-0 specification 1.php must be consistent with the path 2. Class names must be capitalized 3. PHP files other than entry files must have only one class and no other executable code

image.png

So let’s do that

Guofu @ guofu: / home/below $/ usr/local/default/Example/php5.6 demo_class/bin/PHP. PHP Test \ T_Class: : t1 Test \ T2_Class: : t1 guofu@guofu:/home/wwwroot/default/Example$Copy the code
  • __autoload implements auto-loading of classes, making PHP automatically include class files when it uses the class, rather than loading them all in the first place. This mechanism is called lazy loading

It has the following advantages

  1. There is no need to manually include or require 2 when using classes. Because of this lazy loading, unnecessary files 3 are avoided. We can load files according to the namespace, regardless of the actual address of the file.
  • But there’s a problem with that, we knowfunction __autoload($class)Autoload () is a global function that can only be defined once, so it is not flexible enough, so all the logic rules corresponding to class names and file names must be implemented in a function, resulting in a bloated function. , can not be implemented, how to do? Spl_autoload_register () satisfies this requirement after PHP5.3. So let’s see what happens
  • Or use anonymous functions
<? php /** * Notes: * User: zhangguofu * Date: 2021/7/23 * Time: Spl_autoload_register (function ($Class){require_once) (str_replace("\","/",__DIR__."/".$class.".php")); }); \Test\T_Class::t1(); echo PHP_EOL; \Test\T2_Class::t1();Copy the code
  • Multiple functions are also possible

If you’re curious, how does spl_autoload_register work? The spl_autoload_register function registers an incoming function (either a callback or a function name) in the SPL __autoload queue and removes the system’s default __autoload() function.

Once the spl_Autoload_register () function is called, all functions registered with the spl_Autoload_register () function are called in order when the undefined class is called, instead of the __autoload() function being called automatically.

  • So let’s start with SPL, which stands for Standard PHP Library. It is an extension library introduced in PHP5. This library also implements some common data structures, such as queue stack stack, fixed length array and other functions. If you want to go further, you can go to the official website for further understanding

Previous articles are recommended

Php-cgi is the FastCGI manager for PHP? Wrong! Take a step by step in-depth analysis of PHP-FMP phper to learn the practices of Go defer, Panic and Recover, and finally open 2 million corines with CPU increase 93% 叒 to ask you about TCP three-way handshake and four-way wave? Look here! There are pictures and truth!! OpenResty + Lua access Redis in milliseconds. How to optimize transaction isolation level of mysql database