Search code examples
phpsplspl-autoload-register

PHP autoload class not found


I'm having a problem with PHP autoload with a specific class, where it keeps saying it's not found.

Error:

Warning: require_once(Models/BaseModel.php): failed to open stream: No such file or directory in /var/www/html/icompare/init.php on line 29

Fatal error: require_once(): Failed opening required 'Models/BaseModel.php' (include_path='.:/usr/share/php:/var/www/html/icompare/lib') in /var/www/html/icompare/init.php on line 29

How my autoload is started in init.php:

set_include_path(get_include_path().PATH_SEPARATOR.APP_ROOT_PATH.DIRECTORY_SEPARATOR.'lib');
spl_autoload_extensions(".php");
spl_autoload_register(function($className){
  $className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
  $file = $className.".php";
  // echo 'Loading file: '.$file.PHP_EOL;
  require_once $file;
});

My folder structure:

enter image description here

BaseModel class (it's not the full file):

<?php
/**
 * Model Base CRUD Banco de Dados
 */
namespace Models;

class BaseModel {
  protected $tableName = null;

  public function find($value, $field = 'id', $fieldType = \PDO::PARAM_STR){
    if(!isset($this->tableName) || empty($this->tableName)){
      return null;
    }

    $DB = new \DB;
    $sql = sprintf("SELECT * FROM %s WHERE %s = :value", $this->tableName, $field);
    $stmt = $DB->prepare($sql);
    $stmt->bindParam(':value', $value, $fieldType);
    $stmt->execute();

Somehow PHP is looking for a "Models/" capitalized folder? Have you guys ever experienced that before?


Solution

  • You're generating the filename from the classname by doing a string replace of backslash to slash:

      $className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
    

    Since the classname you're trying to load is \Models\BaseModel, the filename that gets generated is .../Models/BaseModel.php. However, your file's actual name is .../models/BaseModel.php, so the require fails because unixy operating systems have case sensitive filenames. If you follow PSR-4 (which you should) then your namespaces should match your directories and your files should match your classes. Thus, the class \Models\BaseModel should be defined in a file named .../Models/BaseModel.php.