Skip to content

popphp/pop-loader

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
November 15, 2022 09:52
src
November 15, 2022 23:38
June 24, 2015 10:47
November 15, 2022 09:52
December 1, 2016 12:03
November 15, 2022 09:52
February 11, 2021 23:20

pop-loader

Build Status Coverage Status

OVERVIEW

pop-loader is a component for managing the autoloading of an application. If, for some reason you do not or cannot use Composer, pop-loader provides an alternative with similar features and API. It supports both PSR-4 and PSR-0 autoloading standards. Additionally, there is support for generating and loading class maps, if you are interested in boosting the speed and performance of your application's load times.

pop-loader is a component of the Pop PHP Framework.

INSTALL

Download or clone this repository and follow the examples below to wire up the autoloading required by your application. Or, you can install pop-loader using Composer - ironic, I know :)

composer require popphp/pop-loader

BASIC USAGE

Using PSR-4

Let's say your app contains a src folder with a Test class in it like this:

app/
    src/
        Test.php
<?php
namespace MyApp;

class Test
{

}

Then, you can create an autoloader object and register your application's source with it like this:

require_once __DIR__ . '/../src/ClassLoader.php';

$autoloader = new Pop\Loader\ClassLoader();
$autoloader->addPsr4('MyApp\\', __DIR__ . '/../app/src');

$test = new MyApp\Test();

Using PSR-0

There's also support for older the PSR-0 standard. If the folder structure and class was like this:

app/
    MyApp/
        Test.php
<?php
class MyApp_Test
{

}

Then, you can register it using PSR-0 like this:

require_once __DIR__ . '/../src/ClassLoader.php';

$autoloader = new Pop\Loader\ClassLoader();
$autoloader->addPsr0('MyApp', __DIR__ . '/../app');

$test = new MyApp_Test();

Using a class map

To generate a new class map:

$mapper = new Pop\Loader\ClassMapper(__DIR__ . '/../app/src');
$mapper->writeToFile('classmap.php');
classmap.php
<?php

return [
    'MyApp\Foo\Bar' => '/home/nick/Projects/pop/pop-loader/app/src/Foo/Bar.php',
    'MyApp\Thing' => '/home/nick/Projects/pop/pop-loader/app/src/Thing.php',
    'MyApp\Test' => '/home/nick/Projects/pop/pop-loader/app/src/Test.php'
];

To load an existing class map:

$autoloader = new Pop\Loader\ClassLoader();
$autoloader->addClassMapFromFile('classmap.php');