Previous Next

Loading Files and Classes Dynamically

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

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.

Example #1 Example of the loadFile() Method

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

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.

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.

Example #2 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.

Previous Next