autoloader works for files in root but not for files in subfolders

303
March 19, 2017, at 12:38 PM

I have an autoloader in a file called config.php as follows:

spl_autoload_register(function($className) { 
    $className = ltrim($className, '\\'); 
    $fileName  = ''; 
    $namespace = ''; 
    if ($lastNsPos = strrpos($className, '\\')) { 
        $namespace = substr($className, 0, $lastNsPos); 
        $className = substr($className, $lastNsPos + 1); 
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; 
    } 
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; 
    require $fileName; 
});

My folder structure looks like this:

The blacked out folder is the project's name. Notice that config.php is located in the "includes" subfolder. It autoloads classes just fine for index.php by including the line require_once ('includes/config.php'); in the script. A new file, functions.php, was created to handle communications with the database for AJAX requests. The autoloader works fine for this file so long as it is in the project root and require_once ('includes/config.php'); is included in the script. However, when functions.php gets moved to a subfolder, say the js subfolder, and require_once ('../includes/config.php'); is included in the script, the autoloader does not work. The error message displayed is Warning: require(classes\model\Operation.php): failed to open stream: No such file or directory in C:\xampp\htdocs\xxxxx\OOP\includes\config.php on line 74. Why isn't the autoloader working when functions.php is in a subfolder? I would like it to work for files in the project root AND for files in subfolders.

READ ALSO
How do I authenticate a custom bot connection in PHP?

How do I authenticate a custom bot connection in PHP?

According to the documentation here bots send an HMAC signature with messages so that they can be authenticated

243
What would be the output of this script?

What would be the output of this script?

In the following script

217
VBScript download webpage html

VBScript download webpage html

I have a script that can successfully download a webpage as a HTML file but the page it downloads can only be viewed once you are logged into the websiteI tried using XMLHTTP POST but it still saved the same page saying "error unauthorized access"

478