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

  • SEARCH
  • Home
  • 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 726195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:25:28+00:00 2026-05-14T06:25:28+00:00

I have read about dynamically loading your class files when needed in a function

  • 0

I have read about dynamically loading your class files when needed in a function like this:

function __autoload($className)
{
   include("classes/$className.class.php");
}

$obj = new DB();

Which will automatically load DB.class.php when you make a new instance of that class, but I also read in a few articles that it is bad to use this as it’s a global function and any libraries that you bring into your project that have an __autoload() function will mess it up.

So does anyone know of a solution? Perhaps another way to achieve the same effect as __autoload()? Until I find a suitable solution I’ll just carry on using __autoload() as it doesn’t start becoming a problem until you bring in libraries and such.

Thanks.

  • 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-14T06:25:28+00:00Added an answer on May 14, 2026 at 6:25 am

    I have used the following code to use spl_autoload_register in a way that it will degrade if it isn’t present, and also handle libraries that use __autoload, that you need to include.

    //check to see if there is an existing __autoload function from another library
    if(!function_exists('__autoload')) {
        if(function_exists('spl_autoload_register')) {
            //we have SPL, so register the autoload function
            spl_autoload_register('my_autoload_function');      
        } else {
            //if there isn't, we don't need to worry about using the stack,
            //we can just register our own autoloader
            function __autoload($class_name) {
                my_autoload_function($class_name);
            }
        }
    
    } else {
        //ok, so there is an existing __autoload function, we need to use a stack
        //if SPL is installed, we can use spl_autoload_register,
        //if there isn't, then we can't do anything about it, and
        //will have to die
        if(function_exists('spl_autoload_register')) {
            //we have SPL, so register both the
            //original __autoload from the external app,
            //because the original will get overwritten by the stack,
            //plus our own
            spl_autoload_register('__autoload');
            spl_autoload_register('my_autoload_function');      
        } else {
            exit;
        }
    
    }
    

    So, that code will check for an existing __autoload function, and add it to the stack as well as your own (because spl_autoload_register will disable the normal __autoload behaviour).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a lot about the Java class loading process lately. Often I
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read in this sitepoint page and quirksmode page about the new :empty
I have read a lot about how to validate dynamically generated content in mvc
I feel bad about not getting this. But, although I have read several articles
I have read a few articles online about optimizing javascript loading. A few key
I have read about continuency parser and dependency parser. but confused which could be
I have read about a lot of people having problems with the browser not
I have read about session fixation and from what I understand it forces a
I have read about LDAP on wikipedia and I kind of understand what it

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.