Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoload classes and functions from different files and directories

I have this autoload code :

function __autoload($class_name)
{
    //class directories
    $directorys = array(
        './Controls/',
        './Config/',
        './Utility/'
    );
    //for each directory
    foreach($directorys as $directory)
    {
        //see if the file exsists
        if(file_exists($directory.$class_name . '.php'))
        {
            require_once($directory.$class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else
            return;
        }
    }
}

I have three directories that they're holding all of my classes and functions.

Can I create a single autoload file in Controls directory and use it to load all function or classes in other php files I mean for example index.php file in /portal/main/index.php

is it possible to load classes that are controls and config in index.php file without including any file in above of index.php file

I mean autoload automatically understands which file is requesting a class or function and include that file for it.

updated code :

function __autoload($class_name)
{
    //class directories
    $directorys = array(
        '/Controls/',
        '/Config/',
        '/Utility/'
    );
    //for each directory

    $ds = "/"; //Directory Seperator
    $dir = dirname(__DIR__); //Get Current file path
    $windir = "\\"; //Windows Directory Seperator
    $path = str_replace($windir, $ds, $dir);

    foreach($directorys as $directory)
    {
        //see if the file exsists
        if(file_exists( $path . $directory . $class_name . '.php'))
        {
            require_once( $path . $directory . $class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else
            return;
        }
    }
}

I have updated the code and it include files, but the only problem that i have is that this function is not running automatically,

for example my autoload file is in : root/Controls/autoload.php and I need some classes and functions in :root/portal/index.php

when i define classes in index.php i get error that the file is not exists and i should manually call autoload.php file in index.php

how can i make autoload smart which i shouldn't have include it in each file for including classes ?

please help me out. thanks in advance

like image 462
anonymox Avatar asked Nov 22 '15 07:11

anonymox


People also ask

What is autoload class in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.

How does PHP autoloader works?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is autoloading classes in Wordpress?

A class autoloader is a system for automatically loading uninstantiated classes, including the files that contain them. When following an established system for file naming and a standard autoloader, you can reliably use any class, without manually including the file.


1 Answers

Simple manual solution : put your autoload file in your project root and include it in your index file this will do the job.

but if you want to use htaccess or php.ini : Place a file called .user.ini into the document root and add the auto_prepend_file directive in there:

auto_prepend_file = /home/user/domain.com/init.php

The file must be inside PHP's include_path. So you must either set the file's directory to be in the include_path inside php.ini, or do it in the .htaccess with a php_value statement.

php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php

If you use the above method in .htaccess, be sure to copy the include_path from php.ini in and add the :/path_to/file_directory so you don't lose any already needed includes.

Alternatively, just add :/path/to/file_directory to include_path directly in the php.ini

Update

If you cannot modify the include_path, you might try specifying a relative path to the auto_prepend_file. This should work since the file path sent is processed identically as if it was called with require():

php_value auto_prepend_file "./file.php"
like image 120
Mohammad_Hosseini Avatar answered Oct 25 '22 12:10

Mohammad_Hosseini