May 15, 2013

How to import all classes within filesystem in php?

J Moore’s Question:

The folder structure is like so:

/main.php
  /lib/class1.php
  /lib/class2.php
  /lib/class3.php

I want to have main.php make available all the classes in lib without doing a ton of require/include. Is this possible? Is it possible to just include all files within a directory?

Create an autoloading function to load the class directly from your URL

function __autoload($class_name) {
    include "/lib/".$class_name . '.php'; //Add your folder structure like this
            // ^ Change the path to your specific need
}

//Then Simply
$class1_object = new Class1(); 

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!