Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Loader and PluginLoader of Zend Framework

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Zend Framework Loader and PluginLoader how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows:

Zend Framework provides Zend_Loader for dynamically loading files.

The following is the specific usage and implementation:

1. Load Fil

How to use it:

Zend_Loader::loadFile ($filename, $dirs=null, $once=false)

Specific implementation:

/ * Loads a PHP file. This is a wrapper for PHP's include () function. * * $filename must be the complete filename, including any * extension such as ".php". Note that a security check is performed that * does not permit extended characters in the filename. This method is * intended for loading Zend Framework files. * * If $dirs is a string or an array, it will search the directories * in the order supplied, and attempt to load the first matching file. * * If the file was not found in the $dirs, or if no $dirs were specified, * it will attempt to load it from PHP's include_path. * * If $once is TRUE, it will use include_once () instead of include (). * * @ param string $filename * @ param string | array $dirs-OPTIONAL either a path or array of paths * to search. * @ param boolean $once * @ return boolean * @ throws Zend_Exception * / public static function loadFile ($filename, $dirs = null, $once = false) {self::_securityCheck ($filename); / * Search in provided directories, as well as include_path * / $incPath = false; if (! empty ($dirs) & & (is_array ($dirs) | | is_string ($dirs)) {if (is_array ($dirs)) {$dirs = implode (PATH_SEPARATOR, $dirs) } $incPath = get_include_path (); set_include_path ($dirs. PATH_SEPARATOR. $incPath);} / * * Try finding for the plain filename in the include_path. * / if ($once) {include_once $filename;} else {include $filename;} / * If searching in directories, reset include_path * / if ($incPath) {set_include_path ($incPath);} return true;}

Parameter rules:

As with the implementation method, it has the following parameters

The $filename parameter specifies the files that need to be loaded. Note that $filename does not need to specify any path, just the file name. ZF does a security check on the file. $filename can only consist of letters, numbers, hyphens -, underscores _ and English periods. Make up (half a corner). The $dirs parameter is unlimited, and you can use Chinese, etc.

The $dirs parameter is used to specify the directory where the file is located, which can be a string or an array. If NULL, the program will look under the system's include_path to see if the file exists (include_path can be set in php.ini-- Haohappy note), if it is a string or array, it will go to the specified directory to look for it, and then include_path.

The $once argument is of Boolean type, if the file is loaded with the PHP function »include_once () for TRUE,Zend_Loader::loadFile (), otherwise it is the PHP function »include (). (this parameter can only be true or false, which is the same as include () and include_once (). )

two。 Load CLA

Specific use:

Zend_Loader::loadClass ('Container_Tree', array (' / home/production/mylib','/ home/production/myapp'))

Specific implementation:

/ * Loads a class from a PHP file. The filename must be formatted* as "$class.php". * * If $dirs is a string or an array, it will search the directories* in the order supplied, and attempt to load the first matching file.** If $dirs is null, it will split the class name at underscores to* generate a path hierarchy (e.g., "Zend_Example_Class" will map* to "Zend/Example/Class.php"). * * If the file was not found in the $dirs, or if no $dirs were specified * it will attempt to load it from PHP's include_path.** @ param string $class-The full class name of a Zend component.* @ param string | array $dirs-OPTIONAL Either a path or an array of paths* to search.* @ return void* @ throws Zend_Exception*/public static function loadClass ($class, $dirs = null) {if ($class, false) | | interface_exists ($class, false) {return } if ((null! = = $dirs) & &! is_string ($dirs) & &! is_array ($dirs)) {require_once 'Zend/Exception.php'; throw new Zend_Exception (' Directory argument must be a string or an array') } / / Autodiscover the path from the class name / / Implementation is PHP namespace-aware, and based on / / Framework Interop Group reference implementation: / / http://groups.google.com/group/php-standards/web/psr-0-final-proposal $className = ltrim ($class,'\'); $file =''; $namespace =''; if ($lastNsPos = strripos ($className,'\')) {$namespace = substr ($className, 0, $lastNsPos) $className = substr ($className, $lastNsPos + 1); $file = str_replace ('\', DIRECTORY_SEPARATOR, $namespace). DIRECTORY_SEPARATOR;} $file. = str_replace ('_', DIRECTORY_SEPARATOR, $className). '.php'; if (! empty ($dirs)) {/ / use the autodiscovered path $dirPath = dirname ($file); if (is_string ($dirs)) {$dirs = explode (PATH_SEPARATOR, $dirs);} foreach ($dirs as $key = > $dir) {if ($dir = ='.) {$dirs [$key] = $dirPath } else {$dir = rtrim ($dir,'\ /'); $dirs [$key] = $dir. DIRECTORY_SEPARATOR. $dirPath;}} $file = basename ($file); self::loadFile ($file, $dirs, true);} else {self::loadFile ($file, null, true);} if (! class_exists ($class, false) & &! interface_exists ($class, false)) {require_once 'Zend/Exception.php' Throw new Zend_Exception ("File\" $file\ "does not exist or class\" $class\ "was not found in the file");}}

The $class class name will correspond to the PHP file in the corresponding directory according to the underscore (as a directory separator) and add '.php', for example, Container_Tree will point to Container\\ Tree.php.

$dir can be an array or a string. The directory is the path to remove the directory contained in the class name.

3. Determine whether a file is readable or not

Specific use:

If (Zend_Loader::isReadable ($filename)) {/ / do something with $filename}

Specific implementation:

/ * Returns TRUE if the $filename is readable, or FALSE otherwise. * This function uses the PHP include_path, where PHP's is_readable () * does not. * * Note from ZF-2900: * If you use custom error handler, please check whether return value * from error_reporting () is zero or not. * At mark of fopen () can not suppress warning if the handler is used. * * @ param string $filename * @ return boolean * / public static function isReadable ($filename) {if (is_readable ($filename)) {/ / Return early if the filename is readable without needing the / / include_path return true } if (strtoupper (substr (PHP_OS, 0,3)) = 'WIN' & & preg_match (' / ^ [amurz]: / iShaw, $filename)) {/ / If on windows, and path provided is clearly an absolute path, / / return false immediately return false;} foreach (self::explodeIncludePath () as $path) {if ($path = ='.') {if (is_readable ($filename)) {return true } continue;} $file = $path. '/'. $filename; if (is_readable ($file)) {return true;}} return false;}

Specific parameters:

The $filename parameter specifies the file name to check, including path information. This method encapsulates the PHP function »is_readable (). Is_readable () does not automatically find files under include_path, but Zend::isReadable () can.

4.Autoloader

The Autoloader function of this class is no longer recommended, so I won't talk about it any more. There are other Autoloader, which will be specified later.

5. Plug-in loader

Specific examples given in the help article are as follows, which can be used for reference:

Many Zend Framework components support plug-ins that allow functions to be loaded dynamically by specifying the prefix of the class and the path to the file to the class (files that do not need to be in include_path or do not need to follow traditional naming conventions). Zend_Loader_PluginLoader provides common functions to do this.

The basic use of PluginLoader follows the naming convention of Zend Framework (one class per file), using an underscore as the path separator when parsing the path. When deciding whether to load a special plug-in class, allow you to pass an optional class prefix for preprocessing. In addition, the paths are searched in LIFO order. Because of the LIFO search and class prefixes, namespaces are allowed to the plug-in, so that the plug-in can be overridden from the path registered earlier.

Basic use case

First, assume the following directory structure and class files, and that the root (toplevel) directory and library directory are in include_path:

Application/

Modules/

Foo/

Views/

Helpers/

FormLabel.php

FormSubmit.php

Bar/

Views/

Helpers/

FormSubmit.php

Library/

Zend/

View/

Helper/

FormLabel.php

FormSubmit.php

FormText.php

Now, create a plug-in loader to make various view helper repositories available:

Then load a given view helper with the prefix defined when the path is added to the class name:

After the class is loaded, you can instantiate.

Note: registers multiple paths for a prefix

Sometimes, when multiple paths use the same prefix, Zend_Loader_PluginLoader actually registers a path array for each given prefix; the last one is checked first, which is useful when you use components in the incubator.

Note: define a path when instantiating

You can provide the constructor with an optional prefix / path pair (or prefix / multiple paths) array parameter:

Zend_Loader_PluginLoader optionally allows sharing of plug-ins without the need for singleton instances, which is done through a static registry, which requires the registry name as the second parameter of the constructor when instantiated:

Other components that use the same name registry to instantiate PluginLoader will have access to loaded paths and plug-ins.

Processing plug-in path

The example in the previous section shows how to add paths to the plug-in loader, so how do you determine which paths have been loaded or delete them?

If $prefix,getPaths ($prefix = null) is not provided, all paths are returned as a "prefix / path" pair, or if $prefix,getPaths ($prefix = null) is provided, returns the path registered for the given prefix.

ClearPaths ($prefix = null) clears all registered paths by default, or if $prefix is provided and placed on the stack, only those paths associated with a given prefix are cleared.

RemovePrefixPath ($prefix, $path = null) allows you to selectively clear specific paths associated with a given prefix. If $path is not provided, all prefix-related paths are cleared, and if $path is provided and the corresponding prefix exists, only this related path is cleared.

Test plug-in and get the name of the class

Sometimes you want to determine whether the plug-in class has been loaded before performing an action, and isLoaded () returns the status of the plug-in name.

Another common use case for PluginLoader is to determine the fully qualified plug-in class name of the loaded class, which getClassName () provides. In general, this is used in conjunction with isLoaded ():

The implementation of the specific plug-in loader can refer to Zend_Loader_PluginLoader and Zend_Loader. There is no repetition here.

Thank you for reading this article carefully. I hope the article "Loader of Zend Framework and how to use PluginLoader" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report