重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。
/** * 获取应用根目录 * @return string */ public static function getRootPath() { if ('cli' == PHP_SAPI) { $scriptName = realpath($_SERVER['argv'][0]); } else { $scriptName = $_SERVER['SCRIPT_FILENAME']; } $path = realpath(dirname($scriptName)); //返回 return $path . DIRECTORY_SEPARATOR; }