AutoLoad mechanism in Zend Framework_PHP tutorial
Code example
<ol class="dp-c"> <li class="alt"><span><span>set_include_path(USVN_LIB_DIR . PATH_SEPARATOR . get_include_path()); </span></span></li> <li><span><span class="keyword">require_once</span><span> </span><span class="string">'Zend/Loader/Autoloader.php'</span><span>; </span></span></li> <li class="alt"><span><span class="vars">$autoloader</span><span> = Zend_Loader_Autoloader::getInstance(); </span></span></li> <li><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"Zend_"</span><span>); </span></span></li> <li class="alt"><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"USVN_"</span><span>); </span></span></li> <li><span><span class="vars">$autoloader</span><span>->registerNamespace(</span><span class="string">"menus_"</span><span>); </span></span></li> <li class="alt"><span><span class="vars">$config</span><span> = </span><span class="keyword">new</span><span> USVN_Config_Ini(USVN_CONFIG_FILE, USVN_CONFIG_SECTION); </span></span></li> </ol>
Process analysis
First, set the include_path, which is the address where the file is searched for when calling include in php
Below It is require_once 'Zend/Loader/Autoloader.php';
In the Zend/Loader/Autoloader.php file, Zend/Loader.php is read. This php defines the Zend_Loader class, which contains The process of instantiating Zend_Loader_Autoloader by loadClass, loadFile, isReadable (whether the file is readable) and other functions is the process of calling its constructor (here the singleton mode is used). The spl_autoload_register(array(__CLASS__, 'autoload') in its constructor ); Use Zend_Loader_Autoloader:autoload as the class autoloading function. I also did an operation to assign _internalAutoloader to its own _autoload
As for how to autoload here, I will check it out based on specific examples
Then I called Zend_Loader_Autoloader:registerNamespace("USVN_" ), what this function does is just mount a value with key USVN_ and value true on the internal attribute _namespaces of Zend_Loader_AutoLoader.
When you see this function, you will understand that the code can also be written as
$autoloader->registerNamespace("Zend_")->registerNamespace("USVN_")
or
$autoloader->registerNamespace(array("Zend_","USVN_"))
Okay, now it’s time to call the USVN_Config_Ini class
This class is a natural choice It is Zend_Loader_Autoloader:autoload("USVN_Config_Ini"). The first step of this function is to call getClassAutoloaders to obtain the AutoLoader of this class. GetClassAutoloaders adds the selection and judgment of namespaceAutoloader. Since we rarely use it, skip the returned loader and print it out like this
Array ( [0] => Zend_Loader_Autoloader Object ( [_autoloaders:protected ] => Array ( ) [_defaultAutoloader:protected] => Array ( [0] => Zend_Loader [1] => loadClass ) [_fallbackAutoloader:protected] => [_internalAutoloader:protected] => Array * RECURSION* [_namespaces:protected] => Array ( [Zend_] => 1 [ZendX_] => 1 [USVN_] => 1 [menus_] => 1 ) [_namespaceAutoloaders:protected] => Array ( ) [_suppressNotFoundWarnings:protected] => [_zfPath:protected] => ) [1] => _autoload )
is actually the _internalAutoloader set earlier.
Here Zend_Loader_Autoloader:_autoload ("USVN_Config_Ini") will actually be called
Okay, now you see the Zend_Loader_Autoloader:_autoload function
$callback = $this-> getDefaultAutoloader();
The default Autoloader will be obtained here. What is the default Autoloader? Look at the initial definition of this class, it is actually array('Zend_Loader', 'loadClass');
below Naturally, the call is call_user_func($callback, $class); that is, Zend_Loader:loadClass("USVN_Config_Ini")
First of all, Zend_Loader has been required in AutoLoader.php
Secondly, let’s take a look Zend_Loader: loadClass method, the first step of this method is to check for exceptions and skip it. The second step is to separate the classes and piece them together into $file, such as USVN/Config/Ini.php. Next, directly call self::loadFile($file, null, true);
Then check self: :loadFile, first _securityCheck checks whether there are illegal characters in the class name. If not, the $file is included. Of course $file here is a relative path and needs to be spliced with include_path. Do you remember where include_path is set? Set it up at the beginning of the program! Okay, here is the USVN_Config_Ini class read in.
You should understand after seeing this. If you define a class yourself and register a Namespace, such as USVN, then you should create a folder with the same name under include_path (case must be distinguished). Then the relative file path name you want to import is read separated by _ in the class name.
This is the end of reading the AutoLoad mechanism.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

Termination Code 0xc000007b While using your computer, you sometimes encounter various problems and error codes. Among them, the termination code is the most disturbing, especially the termination code 0xc000007b. This code indicates that an application cannot start properly, causing inconvenience to the user. First, let’s understand the meaning of termination code 0xc000007b. This code is a Windows operating system error code that usually occurs when a 32-bit application tries to run on a 64-bit operating system. It means it should

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button
