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.
Advertisement
Answer
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(); } }