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

Yaf_Loader::registerLocalNamespace

PHP function Edit on GitHub ✎

(Yaf >=1.0.0)

Register local namespace

Description

Yaf_Loader::registerLocalNamespace(string|array $namespace, string $path = null): Yaf_Loader|false

Register a local namespace. Classes whose prefix belongs to a local namespace are looked up under the given path (or the local library directory when no path is given) instead of the global one.

This lets you place your own classes into the local library directory, while shared classes live in the global library directory (see the yaf.library ini setting).

Parameters

namespace

A namespace prefix string, or an array of prefix/path pairs (when the value is a path string) or prefix strings.

A prefix only matches at an underscore or backslash boundary: registering Models makes Models_User and Models itself local, but not ModelsUser. A leading backslash is ignored, and a class name written with backslashes is looked up as if they were underscores.

path

The directory to look up the namespace in. If not given, the local library directory is used.

Return Values

Returns the loader instance itself on success, or false if an invalid parameter is provided.

Examples

Yaf_Loader::registerLocalNamespace example

php
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
    public function _initLoader()
    {
        $loader = Yaf_Loader::getInstance();

        /* Models_User is now looked up in
         * APPLICATION_PATH/library/Models/User.php */
        $loader->registerLocalNamespace("Models");

        /* register several prefixes at once, each with its
         * own lookup directory */
        $loader->registerLocalNamespace(array(
            "Services" => APPLICATION_PATH . "/services",
            "Vendor"   => APPLICATION_PATH . "/vendor",
        ));
    }
}
?>

See Also

  • Yaf_Loader::getLocalNamespace
  • Yaf_Loader::clearLocalNamespace
  • Yaf_Loader::isLocalName

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