Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:09+00:00 2026-05-26T05:35:09+00:00

Following up on this question , it seems that the duplicate issue could be

  • 0

Following up on this question, it seems that the duplicate issue could be solved by just using the __autoload code below,

function __autoload($class_name) 
{
    include AP_SITE."classes_1/class_".$class_name.".php";
}

$connection = new database_pdo(DSN,DB_USER,DB_PASS);
var_dump($connection);

result,

object(database_pdo)[1]
  protected 'connection' => 
    object(PDO)[2]

but this only loads the classes from one directory, what about other directories? Because I group the classes in different directories. So I will get error if I want to load classes from other directories,

function __autoload($class_name) 
{
    include AP_SITE."classes_1/class_".$class_name.".php";
    include AP_SITE."classes_2/class_".$class_name.".php";
}

message,

Warning:
include(C:/wamp/www/art_on_your_doorstep_2011_MVC/global/applications/CART/classes_2/class_database_pdo.php)
[function.include]: failed to open stream: No such file or directory
in …

which refers to this line – include AP_SITE."classes_2/class_".$class_name.".php";

So, my question is – how can I load classes from multiple directories with __autoload?

a possible solution:

function autoload_class_multiple_directory($class_name) 
{

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

    # Count the total item in the array.
    $total_paths = count($array_paths);

    # Set the class file name.
    $file_name = 'class_'.strtolower($class_name).'.php';

    # Loop the array.
    for ($i = 0; $i < $total_paths; $i++) 
    {
        if(file_exists(AP_SITE.$array_paths[$i].$file_name)) 
        {
            include_once AP_SITE.$array_paths[$i].$file_name;
        } 
    }
}

spl_autoload_register('autoload_class_multiple_directory');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:35:10+00:00Added an answer on May 26, 2026 at 5:35 am

    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');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question I now have code that can attach to a
I'm following along with this useful looking answer to my question . It seems
Following up from this question: How can I unlock a file that is locked
Duplicate - this exact question was asked here - the only solution seems to
I am working on some Python socket code that's using the socket.fromfd() function. However,
I am having a problem with the following query(if this is a duplicate question
Following up on this question: Android: Manage contacts with lookup key (see below) I
Following this question , I am trying to implement an async method using the
Following this question: Good crash reporting library in c# Is there any library like
Following on from this question what would be the best way to write a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.