Autoload classes from different folders

前端 未结 12 1005
不知归路
不知归路 2020-11-28 19:35

This is how I autoload all the classes in my controllers folder,

# auto load controller classes
    function __autoload($class_name) 
    {
             


        
12条回答
  •  广开言路
    2020-11-28 20:05

    I use this. Basically define your folder structure (MVC etc) as a constant in a serialised array. Then call the array in your autoload class. Works efficiently for me.

    You could obviously create the folder array using another function but for MVC you may as well type it in manually.

    For this to work you need to call your classes ...... class.classname.php

      //in your config file
        //define class path and class child folders
        define("classPath","classes");
        define("class_folder_array", serialize (array ("controller", "model", "view")));
    
    
      //wherever you have your autoload class
        //autoload classes
        function __autoload($class_name) {
        $class_folder_array = unserialize (class_folder_array);
        foreach ($class_folder_array AS $folder){
            if(file_exists(classPath."/".$folder.'/class.'.$class_name.'.php')){require_once classPath."/".$folder.'/class.'.$class_name.'.php';break;}
        }
    
    
    
        }
    

提交回复
热议问题