Best Way To Autoload Classes In PHP

前端 未结 6 660
陌清茗
陌清茗 2020-12-05 10:20

I\'m working on a project whereby I have the following file structure:

index.php
|---lib
|--|lib|type|class_name.php
|--|lib|size|example_class.php
         


        
6条回答
  •  死守一世寂寞
    2020-12-05 11:12

    function __autoload($class_name) {
       $class_name = strtolower($class_name);
       $path       = "{$class_name}.php";
       if (file_exists($path)) {
           require_once($path);
       } else {
           die("The file {$class_name}.php could not be found!");
       }
    }
    

    UPDATE: __autoload() is deprecated as of PHP 7.2

提交回复
热议问题