Create instances of all classes in a directory with PHP

11,362

Solution 1

Something like this ought to get the job done:

<?php

    foreach (glob('test/class/*.php') as $file)
    {
        require_once $file;

        // get the file name of the current file without the extension
        // which is essentially the class name
        $class = basename($file, '.php');

        if (class_exists($class))
        {
            $obj = new $class;
            $obj->OnCall();
        }
    }

Solution 2

This is an old question, but I think this is a better solution.

Just by doing:

use Symfony\Component\ClassLoader\ClassMapGenerator;

var_dump(ClassMapGenerator::createMap(__DIR__.'/library'));

You get:

Array
(
    [Acme\Foo] => /var/www/library/foo/Bar.php
    [Acme\Foo\Bar] => /var/www/library/foo/bar/Foo.php
    [Acme\Bar\Baz] => /var/www/library/bar/baz/Boo.php
    [Acme\Bar] => /var/www/library/bar/Foo.php
)

(taken from the documentation)

Solution 3

Since the Symfony ClassLoader component is deprecated, you can use ClassMapGenerator from Composer.

You need first to add Composer files with:

composer require composer/composer

Then you can use:

$map = ClassMapGenerator::createMap(__DIR__ . '/mydirectory');

You will get the related associative table with fully qualified name and file path.

Solution 4

You make a custom loop with in directory and take all required filenames then make something like this.

if $filename = your filename

require_once $filename;
$className = basename($filename, ".php");
$class = new $className;
$class->OnCall();

Another solution is to store all required filenames in array and then loop its array.

Share:
11,362
James
Author by

James

Updated on June 16, 2022

Comments

  • James
    James almost 2 years

    I have a directory with several PHP files consisting of classes with the same names as the files. (Sample.php's class will be called Sample.)

    Each of these classes have a function called OnCall().

    How can I create an instance of every class in my directory and execute all of their OnCall()s?

    I cannot do it manually ($sample = new Sample(); $sample->OnCall();, etc etc etc) because more classes will be added, edited and deleted by users.

  • Marc
    Marc over 10 years
    similar to how I would go. I recommend @ suppressing errors on the require, then test for access. Otherwise errors will bring the system to a halt.
  • Mark
    Mark over 10 years
    I'm not sure how it could fail to require the file, seeing as glob() has found it (therefore it must exist).
  • Marc
    Marc over 10 years
    Instance of a collision (programmer error). I have built this pattern many times and I always include error suppression+check. Just my $0.02 as I agree that glob() should be solid, php produces "amazing" results sometimes.
  • Benjamin C.
    Benjamin C. over 5 years
    Thanks for your first contribution! :) To make your answer easier to read, please use the code block and even add the syntax highlighting on it!
  • Rico Leuthold
    Rico Leuthold over 4 years
    This is nice - already available in Laravel, and I suppose in Symfony as well ;-)
  • Nathan Arthur
    Nathan Arthur over 4 years
    Looks like the component has been removed from the latest version of Symfony. Is there another way to get this functionality?
  • Will Shaver
    Will Shaver over 4 years
  • wali razzaq
    wali razzaq over 3 years
    As of today, you can use it \Composer\Autoload\ClassMapGenerator
  • aariow
    aariow almost 3 years
    You can remove require_once if you're using composer . In addition, there are some PHP functions like spl_autoload_register() and __autoload() (deprecated as of PHP 7.2.0) Autoloading Classes.