PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Loading Classes

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

Example 530. Example of the loadClass() Method

<?php
Zend_Loader
::loadClass('Container_Tree',
    array(
        
'/home/production/mylib',
        
'/home/production/myapp'
    
)
);

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

Zend Framework