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 8354037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:26:07+00:00 2026-06-09T09:26:07+00:00

I am using spl_autoload_register() function to include all files . What i want that

  • 0

I am using spl_autoload_register() function to include all files . What i want that any class having extension .class.php or .php would includes directly. I made below class and register two different function and everything working fine, BUT

I think there is some way so that i need to register only one function to include both extensions together.

please have a look on my function and tell me what i am missing

my folder structure

project
      -classes
           -alpha.class.php
           -beta.class.php
           -otherclass.php
      -includes
           - autoload.php
      -config.inc.php // define CLASS_DIR and include 'autoload.php'

autoload.php

var_dump(__DIR__); // 'D:\xampp\htdocs\myproject\includes' 
var_dump(CLASS_DIR); // 'D:/xampp/htdocs/myproject/classes/' 
spl_autoload_register(null, false);
spl_autoload_extensions(".php, .class.php"); // no use for now
/*** class Loader ***/
class AL
{
     public static function autoload($class)
     {
          $filename = strtolower($class) . '.php';
          $filepath = CLASS_DIR.$filename;
          if(is_readable($filepath)){
              include_once $filepath;
          }
//          else {
//                trigger_error("The class file was not found!", E_USER_ERROR);
//            }
    }

    public static function classLoader($class)
    {
        $filename = strtolower($class) . '.class.php';
        $filepath = CLASS_DIR . $filename;
        if(is_readable($filepath)){
              include_once $filepath;
          }
    }

}
spl_autoload_register('AL::autoload');
spl_autoload_register('AL::classLoader');

Note : there is no effect on line spl_autoload_extensions(); . why?

i also read this blog but did not understand how to implement.

  • 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-06-09T09:26:08+00:00Added an answer on June 9, 2026 at 9:26 am

    There is nothing wrong with the way you do it. Two distinctive autoloaders for two kinds of class files are fine, but I would give them slightly more descriptive names 😉

    Note : there is no effect on line spl_autoload_extensions(); . why?

    This only affects the builtin-autoloading spl_autoload().

    Maybe it’s easier to use a single loader after all

     public static function autoload($class)
     {
          if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
              include_once CLASS_DIR.strtolower($class) . '.php';
          }  else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
              include_once CLASS_DIR.strtolower($class) . '.class.php';
          }
    }
    

    You may also omit the whole class

    spl_autoload_register(function($class) {
        if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
            include_once CLASS_DIR.strtolower($class) . '.php';
        }  else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
            include_once CLASS_DIR.strtolower($class) . '.class.php';
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP Doctrine and i've setup autoloading: spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); I can
I wrote a script that is using the FilterIterator class that comes from the
what is/are the benefit(s) of having multiple spl_autoload_register example: spl_autoload_register('autoload_systems'); spl_autoload_register('autoload_thirdparties'); spl_autoload_register('autoload_services'); vs: using
I want to catch all PHP errors (E_ERROR, E_WARNING, E_NOTICE,..) in a page and
In PHP how do i know if none of the functions registered using spl_autoload_register
Using a populated Table Type as the source for a TSQL-Merge. I want to
Using the Redis info command, I am able to get all the stats of
Using Rails 3.2.0 with haml and sass: I Would like to link an external
I am using some method to autoload helper files with functions. The only problem
I'm using spl_autoload_register to load certain classes when they are needed, but how can

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.