0

So i have an mvc php project and i have a core.php that has an __autoload function that loads my controller and model classes like this:

 <?php
 function __autoload($classname) {
  if (strhas($classname, "Model")) {
    $filename = str_replace("Model", "", $classname);
    $filename = strtolower($filename);
    require_once("mvc/model/$filename.php");
    return;
  }

  if (strhas($classname, "Controller")) {
    $filename = str_replace("Controller", "", $classname);
    $filename = strtolower($filename);
    require_once("mvc/controller/$filename.php");
    return;
  }
}

but after installing JSON web token (JWT) and composer my __autoload function no longer works and my controllers are no longer found.

this is my project structure:

index.php
system/
 -core.php
 -loader.php
 -...
mvc/
 -controller/
  --...
 -model/
  --...
...
tereško
  • 58,060
  • 25
  • 98
  • 150
Raman
  • 21
  • 3
  • May be worth looking at how to use the autoloading within compose - https://stackoverflow.com/questions/12818690/using-composers-autoload – Nigel Ren May 25 '20 at 14:54

1 Answers1

0

That's precisely the drawback of __autoload() and the reason for this remarks in the manual:

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

Tip Although the __autoload() function can also be used for autoloading classes and interfaces, it's preferred to use the spl_autoload_register() function. This is because it is a more flexible alternative (enabling for any number of autoloaders to be specified in the application, such as in third party libraries). For this reason, using __autoload() is discouraged and deprecated as of PHP 7.2.0.

You can either use the recommended replacement or configure Compose to load your own custom classes (your schema looks similar to PSR-4 so it should work with minor changes).

Álvaro González
  • 142,137
  • 41
  • 261
  • 360