top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How we load all classes that placed in different directory in one PHP File , means how to do auto load classes

+2 votes
422 views
How we load all classes that placed in different directory in one PHP File , means how to do auto load classes
posted Dec 29, 2014 by Rahul Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You should name your classes so the underscore (_) translates to the directory separator (/). A few PHP frameworks do this, such as Zend and Kohana.

So, you name your class Model_Article and place the file in classes/model/article.php and then your autoload does...

function __autoload($class_name) 
{
    $filename = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class_name)).'.php';

    $file = AP_SITE.$filename;

    if ( ! file_exists($file))
    {
        return FALSE;
    }
    include $file;
}

Also note you can use spl_autoload_register() to make any function an autoloading function. It is also more flexible, allowing you to define multiple autoload type functions.

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

answer Jan 10, 2015 by Vrije Mani Upadhyay
Similar Questions
+2 votes

I wrote a script, but when searched brings only one set of results. How do I make it so if there is more than one result, it will display them all?

+2 votes

Can someone give me an understanding of how the .ini settings are located and combined? I am under the impression that there is a full settings .ini file somewhere up high in my host's server tree and that
any settings I create in .ini files in each of my domain folders are appended/updated against the 'main' ini settings to give me a 'current' group of php.ini settings.

What I'm looking to find out is does an ini setting established in a test subdomain of my site affect those ini settings outside of my test subdomain?

...