Implementation of automatic loading of PHP Namespace and Class
This article mainly explains the "PHP namespace and class automatic loading implementation method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP namespace and class automatic loading implementation method" bar!
1. Talking about the introduction of documents.
Before PHP 5.3, to introduce code (usually a function or class) defined in another PHP script into one PHP script, you need to use statements such as include, require, include_once, require_once, and so on. Both include and require can introduce a PHP script by specifying a path. The difference is that include issues a warning (E_WARNING) when the corresponding path script is not found, while require throws a fatal error (E_COMPILE_ERROR). Include_once/require_once is also used to introduce the specified path PHP script. The difference with include/require is that if the specified path has already been included, it will not be included again, in other words, it will only contain the same path script once. Include_once and require_once have the same difference as include/require.
So from a performance point of view, it is better to use include_once/require_once, but whether to use include_once or require_once depends on your expected processing that the specified path PHP script does not exist.
In the previous job, we have used them many times to introduce other PHP script files. For example, in the blog project entry file index.php, we introduce bootstrap.php with the following code to introduce the initialization function bootApp to call: