Using too much include() in php

前端 未结 3 759
北荒
北荒 2021-01-18 04:30

I have a habit of using include() a lot in my php scripts. I would like to know is it a good approach. I just use include a lot because it makes code look-better for future-

3条回答
  •  星月不相逢
    2021-01-18 05:19

    If you're developing object-orientated and have a file for each class, consider implementing an autoloader function that automatically calls include when a class is used but not yet loaded:

    $callback = function($className) {
        // Generate the class file name using the directory of this initial file
        $fileName = dirname(__FILE__) . '/' . $className . '.php';
        if (file_exists($fileName)) {
            require_once($fileName);
            return;
        }
    };
    
    spl_autoload_register($callback);
    

提交回复
热议问题