Including a whole directory in PHP or Wildcard for use in PHP Include?

后端 未结 5 629
粉色の甜心
粉色の甜心 2021-01-12 07:30

I have a command interpreter in php. It lives inside the commands directory and needs access to every command in the command file. Currently I call require once on each co

5条回答
  •  渐次进展
    2021-01-12 07:51

    Why do you want to do that? Isn't it a better solution to only include the library when needing it to increase speed and reduce footprint?

    Something like this:

    Class Interpreter 
    {
        public function __construct($command = null)
        {
            $file = 'Command'.$command.'.php';
    
            if (!file_exists($file)) {
                 throw new Exception('Invalid command passed to constructor');
            }
    
            include_once $file;
    
            // do other code here.
        }
    }
    

提交回复
热议问题