PHPackages                             morningtrain/php-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. morningtrain/php-loader

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

morningtrain/php-loader
=======================

A class for loading PHP files and classes in a PSR-4 directory

v0.3.3(3y ago)13.8k18MITPHPPHP ^8.0

Since Jun 14Pushed 3y ago4 watchersCompare

[ Source](https://github.com/Morning-Train/php-loader)[ Packagist](https://packagist.org/packages/morningtrain/php-loader)[ RSS](/packages/morningtrain-php-loader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (9)Used By (8)

PHP Loader
==========

[](#php-loader)

A simple PHP File or class loader for PHP. Built with PHP.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Getting Started](#getting-started)
    - [Installation](#installation)
- [Dependencies](#dependencies)
    - [symfony/finder](#symfonyfinder)
- [Usage](#usage)
    - [Multiple Paths](#multiple-paths)
- [Filename Constraints](#filename-constraints)
    - [Using Multiple file names](#using-multiple-file-names)
- [Loading Classes](#loading-classes)
    - [Has Method](#has-method)
    - [Call Static](#call-static)
    - [Class or inheritance requirement](#class-or-inheritance-requirement)
    - [Constructing, invoking or calling](#constructing-invoking-or-calling)
- [Credits](#credits)
- [Testing](#testing)
- [License](#license)

Introduction
------------

[](#introduction)

This package is a tool to help you initialize parts of your projects by loading in all files that match certain filename rules in a defined directory.

You may even initialize the classes contained in these files as long as they are PSR-4 compliant.

For instance, you may use this tool to load all files in a "/routes" directory or all files ending with "Block.php" and initialize all found classes that extend `Block` and then call `init` on them.

More specifically, this tool is made for, but not dependent on, the WP-Framework. Here it is useful for loading in all routes, registering all blocks and initializing all Hooks.

Getting Started
---------------

[](#getting-started)

To get started install the package as described below in [Installation](#installation).

To use the tool have a look at [Usage](#usage)

### Installation

[](#installation)

```
composer require morningtrain/php-loader
```

Dependencies
------------

[](#dependencies)

### symfony/finder

[](#symfonyfinder)

[Finder](https://symfony.com/doc/current/components/finder.html) is used to find files in the directory

Usage
-----

[](#usage)

First create a Loader using `Loader::create`. This takes an absolute path to the directory you wish to load from as an argument and returns a Loader. The Loader is further configured by chaining.

In its simples form the Loader only needs a path. This will tell it to load all .php files in that directory using `require`

```
    // Loading all PHP files in ./MyDir
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir');
```

### Multiple Paths

[](#multiple-paths)

You may supply an array of full paths to `Loader::create` if you need to handle multiple directories;

```
    // Loading all PHP files in ./MyDir and ./MyOtherDir
    use Morningtrain\PHPLoader\Loader;

    Loader::create([__DIR__ . '/MyDir',__DIR__ . '/MyOtherDir']);
```

Filename Constraints
--------------------

[](#filename-constraints)

To limit the loader to only load files with a given name use `fileName(string|array $filename)`See [Symfoni Finder: File Name](https://symfony.com/doc/current/components/finder.html#file-name) for options.

By default `$fileName` is `*.php`

```
    // Loading all PHP files that end with "Foo" in ./MyDir
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir')
        ->fileName('*Foo.php');
```

### Using Multiple file names

[](#using-multiple-file-names)

If you need to allow multiple filename formats then supply an array for `Loader::fileName`

Loading Classes
---------------

[](#loading-classes)

As long as no class related options are set on the Loader it will simply load the files.

This is useful for route files and similar.

If you have classes that you wish to load and initialize then read on!

**Note:**All files will be loaded even if the class requirements are not fulfilled. The Loader has no knowledge of its classes before they are loaded.

### Has Method

[](#has-method)

Aborts handling a found class if it does not have a specific method.

**Note:** it is not necessary to specify `hasMethod` if `call` or `callStatic` is used.

```
    // Loading all PHP files in ./MyDir and invoke them if they have the method myMethod
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir')
        ->hasMethod('myMethod')
        ->invoke();
```

### Call Static

[](#call-static)

To call a static method on all loaded classes specify the method using `Loader::callStatic($methodName)`

This will call said method on every loaded class that has it. You do not need to check using `Loader::hasMethod`beforehand

```
    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir')
        ->isA(\Foo::class)
        ->callStatic('myMethod');
```

### Class or inheritance requirement

[](#class-or-inheritance-requirement)

To only call methods on classes that are of a given class or extended from it use `Loader::isA($className)`. This works the same way is `ia_a($obj,$class)` where $obj is the found class.

If the found class does not match the required class then the Loader will stop handling the current class and the class will never be constructed or called.

```
    // Loading all PHP files in ./MyDir and call a static method on it if it is of the class Foo
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir')
        ->isA(\Foo::class)
        ->callStatic('myMethod');
```

### Constructing, invoking or calling

[](#constructing-invoking-or-calling)

You can also construct an instance from the loaded classes, call a method on an instance or invoke an instance using the Loader.

If you use `Loader::invoke` or `Loader::call` then it is not necessary to use `Loader::construct` as well

```
    // Loading all PHP files in ./MyDir, construct them and then call 'myMethod'
    use Morningtrain\PHPLoader\Loader;

    Loader::create(__DIR__ . '/MyDir')
        ->call('myMethod');
```

Credits
-------

[](#credits)

- [Mathias Munk](https://github.com/mrmoeg)
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Recently: every ~20 days

Total

8

Last Release

1354d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/278725?v=4)[morningtrain](/maintainers/morningtrain)[@morningtrain](https://github.com/morningtrain)

![](https://www.gravatar.com/avatar/dc62314c2179f47535e9ad94944210faf9dcab3601d340487bd79f21a6b654bf?d=identicon)[mrmoeg](/maintainers/mrmoeg)

---

Top Contributors

[![mrmoeg](https://avatars.githubusercontent.com/u/5990117?v=4)](https://github.com/mrmoeg "mrmoeg (41 commits)")

---

Tags

loadermorningtrain

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/morningtrain-php-loader/health.svg)

```
[![Health](https://phpackages.com/badges/morningtrain-php-loader/health.svg)](https://phpackages.com/packages/morningtrain-php-loader)
```

###  Alternatives

[api-platform/symfony

Symfony API Platform integration

323.2M67](/packages/api-platform-symfony)[h4cc/alice-fixtures-bundle

Symfony2 Bundle for loading fixture data with the Alice library.

76314.2k7](/packages/h4cc-alice-fixtures-bundle)[boekkooi/twig-jack-bundle

Handy additional features for Twig within symfony 2

2323.0k](/packages/boekkooi-twig-jack-bundle)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
