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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:33:50+00:00 2026-05-25T12:33:50+00:00

I have the code below as an autoload class, however it appears that the

  • 0

I have the code below as an autoload class, however it appears that the clean method simply isn’t working and it always falls back on the dirty method.

Am I using spl_autoload incorrectly? If so what is the correct (better) way? Is this inefficient, how could it be improved?

I always get output such as the bottom when using this method though, and in some cases it just doesn’t find the class but doesn’t throw any error I have display errors set to 1 and have checked the error log but just completely missing.

The code gets initialised as

require "vendor/AutoLoader.class.php";
self::setGlobal("autoloader", AutoLoader::init());

And the class is as follows:

public static $instance;
private $_src=array('vendor/', 'lib/', '');
private $_sub=array('base/', '');
private $_ext=array('.php', 'class.php', 'lib.php');

/* initialize the autoloader class */
public static function init(){
    if(self::$instance==NULL){
        self::$instance=new self();
    }
    return self::$instance;
}

/* put the custom functions in the autoload register when the class is initialized */
private function __construct(){
    spl_autoload_register(array($this, 'clean'));
    spl_autoload_register(array($this, 'dirty'));
}

/* the clean method to autoload the class without any includes, works in most cases */
private function clean($class){
    $class=str_replace('_', '/', $class);
    spl_autoload_extensions(implode(',', $this->_ext));
    foreach($this->_src as $resource){
      foreach($this->_sub as $sub){
        echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
        set_include_path(pegFramework::getGlobal("baseDir") . $resource.$sub);
        spl_autoload($class);
        if(class_exists($class)) {
          echo 'Found and clean included '.$class.' in '.$resource.$sub."<br />";
          break 2;
        }
      }
    }
}

/* the dirty method to autoload the class after including the php file containing the class */
private function dirty($class){
    global $docroot;
    $class=str_replace('_', '/', $class);
    foreach($this->_src as $resource){
        foreach($this->_ext as $ext){
          foreach($this->_sub as $sub){
            echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
            if(@include(pegFramework::getGlobal("baseDir") . $resource . $sub. $class . $ext)) {
              echo 'Found and dirty included '.$class.' as '.$resource . $sub. $class . $ext."<br />";
              break 3;
            }
          }
        }
    }
    spl_autoload($class);
}

Trying to load pegDatabase via pegAutoloader::clean()
...snip...
Trying to load pegDatabase via pegAutoloader::clean()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load basepegDatabase via pegAutoloader::clean()
...snip...
Trying to load basepegDatabase via pegAutoloader::clean()
Trying to load basepegDatabase via pegAutoloader::dirty()
Found and dirty included basepegDatabase as vendor/base/basepegDatabase.php
Found and dirty included pegDatabase as vendor/pegDatabase.php
Trying to load pegRequest via pegAutoloader::clean()
...snip...
Trying to load pegRequest via pegAutoloader::clean()
Trying to load pegRequest via pegAutoloader::dirty()
Trying to load pegRequest via pegAutoloader::dirty()
Found and dirty included pegRequest as vendor/pegRequest.php
Trying to load pegFacebook via pegAutoloader::clean()
...snip...
Trying to load pegFacebook via pegAutoloader::clean()
Trying to load pegFacebook via pegAutoloader::dirty()
...snip...
Trying to load pegFacebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::clean()
Trying to load Facebook via pegAutoloader::clean()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
  • 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-25T12:33:51+00:00Added an answer on May 25, 2026 at 12:33 pm

    I think it’s the call to spl_autoload that’s tripping you up. In the comments of the php documentation, I found this:

    Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php

    So, calls to spl_autoload with pegFacebook or Facebook are just going to trigger searches for files named pegfacebook.php or facebook.class.php.

    If I were you, though, I would look at Symfony’s UniversalClassLoader for examples of much more simplified handling of class loading. You could probably tweak it to use specific extensions, but I would at least think about finding a new way to handle this problem.

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

Sidebar

Related Questions

I have noticed that my geocoder is inconsistent in the code shown below because
EDIT: See my working code in the answers below. In brief: I have a
I have code below that is getting called from a while loop so it's
I have code below that builds a collection and returns it sorted by a
I have code that overloads operator new . The code below works fine under
i have code like below. Base is the base class and D1, D2, D3
I have the code below: class A { }; class B: public virtual A
I have the code below that lists all the images in a folder, the
I have the code below that should copy the file index.php to the directory
I met an interesting issue about C#. I have code like below. List<Func<int>> actions

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.