Automatically loading the classes
<p>I've built "core" class that loads another classes, and I want to load automatically all the classes in spesific folder named "class", I've started to build something, but I have no idea if it's good. <br>
In the construct function at the core class, I'm getting an array with the class names to load. <br>
The construct function calls to function named _loadClasses, and in the _loadClasses function, <br>I'm loading the classes by using require_once() fucntion. <br>
Then at the top of the page, i'm adding public variable with the name of the class. <br>
For example, "public $example;"<br>
Now, what left is to create the ocject of the class, so that's what I did. <br>
Example of the _loadClasses method: <Br></p>

<pre><code>require_once("class/user.class.php");
self::$user = new User();
</code></pre>

<p>Here comes the "automat" part. <br>
I want the function _loadClasses to get an array, for example: <br></p>

<pre><code>private function _loadClasses( $classesToLoad = array('security', 'is') );
</code></pre>

<p>and now, I'm using glob to load the classes from the folder "class", the name syntax of the classes files in the folder "class" is classname.class.php. <br></p>

<pre><code>$classesArray = array(); // initialize the variable of all the web classes

    $classesFiles = glob("class/*.php"); // gets all the web classes from the folder 'class'

    foreach($classesFiles as $file) { // loop on the classes in the folder 'class'
        $filename = explode('class/', $file);
        $filename = $filename[1];
        $className = explode('.class.php', $filename);
        $className  = $className[0];

        if($className != 'index.php' || $className != 'database') {
            array_push( $classesArray, $className ); // adds the class name into the array 'classesArray'
        }
    }

    foreach( $classesArray as $className ) {
        if( in_array($className, $classesToLoad) ) {
            require_once("class/$className.class.php");
            $classLines = file( "class/$className.class.php" );
            $classNameLine = $classLines[1];
            $classNameLine = explode(' ', $classNameLine);
            $classObjectName = $classNameLine[1];
            $classObjectName = explode(" ", $classObjectName);


            self::$$classObjectName = new $classObjectName();
        }
    }
</code></pre>

<p>I need something like that, of curse it doesn't work, it's just to show you what I wanna do with an example. <br>Thanks in advance.</p>