Autoload Classes from Different Folders

Autoload classes from different folders

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.

Edit

Note : __autoload has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged. Please refer to PHP documentation for more details. http://php.net/manual/en/function.autoload.php

PHP autoload classes from different directories

You can add the other directories to the include path and if the class files have the same extension as your existing class files, your autoloader will find them

Before calling Autoloader:init(), do:

//directories you want the autoloader to search through
$newDirectories = ['/path/to/a', '/path/to/b'];
$path = get_include_path().PATH_SEPARATOR;
$path .= implode(PATH_SEPARATOR, $newDirectories);
set_include_path($path)

Autoload classes and functions from different files and directories

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"

How can I load classes from multiple directories with __autoload?

You can register multiple autoload functions by using spl_autoload_register instead of the single __autoload function. That's the recommended way.

If one autoloader was able to load the file, the next one in the stack won't be called.

Each autoloader however should only load the classes it is for, so you need to check that by the classname and/or with is_file. By classname often is better because trying wildly on the file-system can stress a system if your application grows.

To not re-invent the wheel, you could even use an autoloader that already exists which is able to deal with the PSR-0 standard on file-name-calling. Those often allow to register a specific namespace on a base-directory. In your case that would mean that you must rename and organize your files according to the PSR-0 convention.


The quick solution (bound to your question):

function __autoload($class_name) 
{
$file = sprintf('%sclasses_1/class_%s.php', AP_SITE, $class_name);
if (is_file($file))
{
include $file;
return;
}
$file = sprintf('%sclasses_2/class_%s.php', AP_SITE, $class_name);
if (is_file($file))
{
include $file;
return;
}
}

As you can see, there is already code duplicated (as in yours). So this should just be a temporary solution as you will end up with more and more duplicated lines for each directory you would like to test for. If you consider to change the design, please take the PSR-0 shema into account, it helps to streamline one's codebase and makes it easy to re-use other existing compontents in the PHP world.


function autoload_class_multiple_directory($class_name) 
{

# List all the class directories in the array.
$array_paths = array(
'classes_1/',
'classes_2/'
);

foreach($array_paths as $path)
{
$file = sprintf('%s%s/class_%s.php', AP_SITE, $path, $class_name);
if(is_file($file))
{
include_once $file;
}

}
}

spl_autoload_register('autoload_class_multiple_directory');

How we load all classes that placed in different directory in one PHP File

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;
}

Example taken from Autoload classes from different folders

Edit#1 Not Tested

spl_autoload_register(function ($class_name) { 

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

$file = AP_SITE.$filename;

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

Laravel autoloading classes within custom folders

I am afraid you are using the incorrect key classmap for this. you should be using psr-4.

For example:

"autoload": {
"psr-4": {
"App\\": "app/",
"Features\\": "app/Features/"
},
"classmap": [ ... ]
},

Then run composer dump-auto.

P.S. putting the Features folder under app folder is a little strange; as app is already mapped to App.



Related Topics



Leave a reply



Submit