Search the Community
Showing results for tags 'instance'.
-
I want to require_once all of the files in my directory and assign instances of their classes to properties in the main class. Main class (Test.php): <?php error_reporting(E_ALL); class Test { public $egg; public function __construct() { $this->getInstances(); $this->egg->msg("Hello!"); // line 11 } public function getInstances($folder = null) { if($folder != null) { foreach(glob("$folder/*.php") as $file) { require_once $file; $class = basename($file); if(class_exists($class)) { $lclass = strtolower($class); if(property_exists($this, $lclass)) { $this->$lclass = new $class(); } } } } else { foreach(glob("*.php") as $file) { require_once $file; $class = basename($file); if(class_exists($class)) { $lclass = strtolower($class); if(property_exists($this, $lclass)) { $this->$lclass = new $class(); } } } } } } $test = new Test(); ?> Sample class (Egg.php): <?php class Egg { public function msg($msg) { echo $msg . chr(10); } } ?> What it outputs: Fatal error: Call to a member function msg() on a non-object in Test.php on line 11 What I want it to output: Hello!