Create instances of all classes in a directory with PHP
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.
James
Updated on June 16, 2022Comments
-
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 calledSample
.)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 over 10 yearssimilar 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 over 10 yearsI'm not sure how it could fail to
require
the file, seeing asglob()
has found it (therefore it must exist). -
Marc over 10 yearsInstance 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. over 5 yearsThanks 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 over 4 yearsThis is nice - already available in Laravel, and I suppose in Symfony as well ;-)
-
Nathan Arthur over 4 yearsLooks like the component has been removed from the latest version of Symfony. Is there another way to get this functionality?
-
Will Shaver over 4 years
-
wali razzaq over 3 yearsAs of today, you can use it
\Composer\Autoload\ClassMapGenerator
-
aariow almost 3 yearsYou can remove
require_once
if you're using composer . In addition, there are some PHP functions likespl_autoload_register()
and__autoload()
(deprecated as of PHP 7.2.0) Autoloading Classes.