php8.5
Home/ Manual/ yaf / yaf_loader/ Yaf_Loader::autoload

Yaf_Loader::autoload

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Autoload a class

Description

Yaf_Loader::autoload(string $class_name): bool

Load a class by its name. The class name is converted to a file path: underscores and namespace separators become directory separators.

MVC classes (classes whose names contain _Controller, _Action or _Plugin) are looked up under the application directory, while other classes are looked up in the library directories: the local library for locally registered class prefixes, otherwise the global library.

Parameters

class_name

The class name to load.

Return Values

Returns true on success, or false on failure.

Examples

Yaf_Loader::autoload example

php
<?php
/*
 * autoload() is normally invoked implicitly by the engine
 * whenever an undefined class is referenced, but it can also
 * be called explicitly to force loading of a class file.
 */
$loader = Yaf_Loader::getInstance("/var/www/shop/application/library");

/* looks up /var/www/shop/application/library/Catalog/PriceService.php */
$loader->autoload("Catalog_PriceService");

/* MVC classes are looked up under the application directory */
$loader->autoload("ProductController");
?>

See Also

  • Yaf_Loader::import
  • Yaf_Loader::registerLocalNamespace

Source: reference/yaf/yaf_loader/autoload.xml · from the official PHP manual (php/doc-en)