Best Way To Autoload Classes In PHP

前端 未结 6 665
陌清茗
陌清茗 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:00

    I have an example here that I use for autoloading and initiliazing.
    Basically a better version of spl_autoload_register since it only tries to require the class file whenever you initializes the class.
    Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.
    index.php

     'test']);
    

    autoload.php

    {$class} doesn't exist."));
        }*/
    }
    

    You can easily manage with a bit of coding, to require classes in different folders too.
    Hopefully this can be of some use to you.

提交回复
热议问题