martin-pettersson/wp-autoloader

此包已被废弃,不再维护。未建议替代包。

WordPress 编码标准的类加载器。

0.2.0 2017-05-11 15:40 UTC

This package is not auto-updated.

Last update: 2019-09-13 00:09:19 UTC


README

WordPress 编码标准的 类加载器

安装

通过 composer
将 WP Autoloader 作为依赖项要求: composer require martin-pettersson/wp-autoloader
包含 composer 自动加载文件: require_once 'path/to/vendor/autoload.php';
手动
下载/克隆仓库

WP Autoloader 支持 PSR-4,因此任何 PSR-4 类加载器都适用,只需添加命名空间 "WPAutoloader" 并将其指向 wp-autoloader/src 目录。

用法

<?php

// Either use composer's autoload feature or your own to make the WP Autoloader available.
require_once 'path/to/vendor/autoload.php';

// Create an instance of the autoloader.
$autoloader = new WPAutoloader;

// Register namespaces to your source code (compliant with the WordPress coding standards)
$autoloader->addNamespaceDirectories('My_Plugin', ['path/to/my/source/files']);

// The autoloader must be activated.
$autoloader->activate();

// The file "path/to/my/source/files/class-a.php" will be included if the class is unavailable.
$a = new My_Plugin\A;

// The file "path/to/my/source/files/nested-namespace/class-b.php" will be included if the class is unavailable.
$b = new My_Plugin\Nested_Namespace\B;

API

void addNamespaceDirectories( string $namespace, array $directories )
为给定的命名空间追加目录。
array getNamespaces( void )
返回所有已注册的命名空间及其相应的目录。
array getNamespaceDirectories( string $namespace )
返回为给定的命名空间注册的目录。
void activate( void )
将类加载器添加到自动加载堆栈。
void deactivate( void )
从自动加载堆栈中移除类加载器。
bool loadClass( string $class )
尝试包含类文件。在成功时返回 true 或在失败时返回 false。