martin-pettersson/wp-autoloader

This package is abandoned and no longer maintained. No replacement package was suggested.

A class loader for the WordPress coding standards.

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

A class loader for the WordPress coding standards.

Installation

Through composer
Require WP Autoloader as a dependency: composer require martin-pettersson/wp-autoloader
Include the composer autoload file: require_once 'path/to/vendor/autoload.php';
Manually
Download/clone the repo

WP Autoloader is PSR-4 compliant so any PSR-4 class loader will do, just add the namespace "WPAutoloader" and point it to the wp-autoloader/src directory.

Usage

<?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 )
Appends directories for the given namespace.
array getNamespaces( void )
Returns all registered namespaces and their respective directories.
array getNamespaceDirectories( string $namespace )
Returns the directories registered for the given namespace.
void activate( void )
Adds the class loader to the autoloading stack.
void deactivate( void )
Removes the class loader from the autoloading stack.
bool loadClass( string $class )
Attempts to include the class file. Returns true on success or false on failure.