Zend Framework の紹介

 Learning Zend Framework


 Zend Framework リファレンス

  • 第36章 Zend_Gdata
  • 第37章 Zend_Http
  • 第38章 Zend_InfoCard
  • 第39章 Zend_Json
  • 第40章 Zend_Layout
  • 第41章 Zend_Ldap
  • 第42章 Zend_Loader
  • 第43章 Zend_Locale
  • 第44章 Zend_Log
  • 第45章 Zend_Mail
  • 第46章 Zend_Markup
  • 第47章 Zend_Measure
  • 第48章 Zend_Memory
  • 第49章 Zend_Mime
  • 第50章 Zend_Navigation
  • 第51章 Zend_Oauth
  • 第52章 Zend_OpenId
  • 第53章 Zend_Paginator
  • 第54章 Zend_Pdf
  • 第55章 Zend_ProgressBar
  • 第56章 Zend_Queue
  • 第57章 Zend_Reflection
  • 第58章 Zend_Registry
  • 第59章 Zend_Rest

  • 第60章 Zend_Search_Lucene
  • 第61章 Zend_Serializer
  • 第62章 Zend_Server
  • 第63章 Zend_Service
  • 第64章 Zend_Session
  • 第65章 Zend_Soap
  • 第66章 Zend_Tag
  • 第67章 Zend_Test
  • 第68章 Zend_Text
  • 第69章 Zend_TimeSync
  • 第70章 Zend_Tool
  • 第71章 Zend_Tool_Framework
  • 第72章 Zend_Tool_Project
  • 第73章 Zend_Translate
  • 第74章 Zend_Uri
  • 第75章 Zend_Validate
  • 第76章 Zend_Version
  • 第77章 Zend_View
  • 第78章 Zend_Wildfire
  • 第79章 Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 70.6% Update 2010-11-28 - Revision 23415

    第42章 Zend_Loader

    42.1. Loading Files and Classes Dynamically

    The Zend_Loader class includes methods to help you load files dynamically.

    [ティップ] Zend_Loader vs. require_once()

    The Zend_Loader methods are best used if the filename you need to load is variable. For example, if it is based on a parameter from user input or method argument. If you are loading a file or a class whose name is constant, there is no benefit to using Zend_Loader over using traditional PHP functions such as require_once().

    42.1.1. Loading Files

    The static method Zend_Loader::loadFile() loads a PHP file. The file loaded may contain any PHP code. The method is a wrapper for the PHP function include(). This method returns boolean FALSE on failure, for example if the specified file does not exist.

    例 42.1. Example of the loadFile() Method


    The $filename argument specifies the filename to load, which must not contain any path information. A security check is performed on $filename. The $filename may only contain alphanumeric characters, dashes ("-"), underscores ("_"), or periods ("."). No such restriction is placed on the $dirs argument.

    The $dirs argument specifies which directories to search for the file in. If the value is NULL, only the include_path is searched; if the value is a string or an array, the directory or directories specified will be searched, followed by the include_path.

    The $once argument is a boolean. If TRUE, Zend_Loader::loadFile() uses the PHP function include_once() for loading the file, otherwise the PHP function include() is used.

    42.1.2. Loading Classes

    The static method Zend_Loader::loadClass($class, $dirs) loads a PHP file and then checks for the existence of the class.

    例 42.2. Example of the loadClass() Method


    The string specifying the class is converted to a relative path by substituting underscores with directory separators for your OS, and appending '.php'. In the example above, 'Container_Tree' becomes 'Container\\Tree.php' on Windows.

    If $dirs is a string or an array, Zend_Loader::loadClass() searches the directories in the order supplied. The first matching file is loaded. If the file does not exist in the specified $dirs, then the include_path for the PHP environment is searched.

    If the file is not found or the class does not exist after the load, Zend_Loader::loadClass() throws a Zend_Exception.

    Zend_Loader::loadFile() is used for loading, so the class name may only contain alphanumeric characters and the hyphen ('-'), underscore ('_'), and period ('.').

    [注意] Loading Classes from PHP Namespaces

    Starting in version 1.10.0, Zend Framework now allows loading classes from PHP namespaces. This support follows the same guidelines and implementation as that found in the PHP Framework Interop Group PSR-0 reference implementation.

    Under this guideline, the following rules apply:

    • Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.

    • Each "_" character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The "_" character has no special meaning in the namespace.

    • The fully-qualified namespace and class is suffixed with ".php" when loading from the file system.

    As examples:

    • \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php

    • \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php

    • \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

    42.1.3. Testing if a File is Readable

    The static method Zend_Loader::isReadable($pathname) returns TRUE if a file at the specified pathname exists and is readable, FALSE otherwise.

    例 42.3. Example of isReadable() method

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

    The $filename argument specifies the filename to check. This may contain path information. This method is a wrapper for the PHP function is_readable(). The PHP function does not search the include_path, while Zend_Loader::isReadable() does.

    42.1.4. Using the Autoloader

    The Zend_Loader class contains a method you can register with the PHP SPL autoloader. Zend_Loader::autoload() is the callback method. As a convenience, Zend_Loader provides the registerAutoload() function to register its autoload() method. If the spl_autoload extension is not present in your PHP environment, then the registerAutoload() method throws a Zend_Exception.

    例 42.4. Example of registering the autoloader callback method


    After registering the Zend Framework autoload callback, you can reference classes from Zend Framework without having to load them explicitly. The autoload() method uses Zend_Loader::loadClass() automatically when you reference a class.

    If you have extended the Zend_Loader class, you can give an optional argument to registerAutoload(), to specify the class from which to register an autoload() method.

    例 42.5. Example of registering the autoload callback method from an extended class

    Because of the semantics of static function references in PHP, you must implement code for both loadClass() and autoload(), and the autoload() must call self::loadClass(). If your autoload() method delegates to its parent to call self::loadClass(), then it calls the method of that name in the parent class, not the subclass.

    class My_Loader extends Zend_Loader
        public static function 
    loadClass($class$dirs null)

        public static function 
            try {
            } catch (
    Exception $e) {


    You can remove an autoload callback. The registerAutoload() has an optional second argument, which is TRUE by default. If this argument is FALSE, the autoload callback is unregistered from the SPL autoload stack.

    digg delicious meneame google twitter technorati facebook