PHP Bootstrapping 更好的方法?

标签 php bootstrapping

引导您的 PHP 应用程序是一种好习惯吗? 我找到了两种引导 PHP 应用程序的方法。需要一些更好的建议。

首先。
为文件夹结构定义一个常量

$controllerPath = 'controller';
define('CONTROLLER', str_replace('\\', '/', realpath($controllerPath)).'/');

//usage
require_once CONTROLLER . 'somecontroller.php';

第二
使用 ini_set 将包含路径设置为应用程序根目录

$rootPath = $_SERVER['DOCUMENT_ROOT'];
$includePath = ini_get('include_path');
ini_set('include_path', '.'.PATH_SEPARATOR.$rootPath.PATH_SEPARATOR.$includePath);

//usage
require_once 'controller/somecontroller.php';

请告诉我哪种方法更好。

如果是高负载应用程序,最好的方法是什么??

最佳答案

使用 ini_set 将其设置到应用程序上方的目录。这就是为什么您可以在 require 语句中使用文字字符串的原因。此外,它使重用代码变得更容易

require 'coolapp/class/Model.php'
require 'coolapp/display/Router.php'
require 'spinoff/display/JsView.php'
// etc

这类似于 java 中完全限定导入 com.whatever.app.more 的想法,或者在 python 中,所有应用导入应该相对于该应用是绝对的。

回复:高负载应用

除非您要加载数千个文件,否则包含文件所需的时间可能不是瓶颈。但是,如果是这种情况,您有几个选择。一种是APC,它将include的结果缓存在内存中。另一种是从单个文件加载所有内容,类似于将 javascript 文件连接成一个以获得更好性能的方式(巧合的是,APC 具有为您提供此信息的功能)。 APC 真的很容易设置并且完全透明,可以提高 ~50% better performance .

关于PHP Bootstrapping 更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/501505/

相关文章:

r - 具有分组/嵌套数据的 Bootstrap 重采样和整洁回归模型

php - PHP如何访问特定的json数据

php - 如何在 codeIgniter 中自定义表单验证错误

php - Cakephp,它有什么用?

php mysql错误,提示插入语句

angularjs - 带有计时器的成功动画 - Ionic 框架上的 Sweet Alert

tomcat - 在 intellij IDEA 社区版中将 Tomcat 作为应用程序运行

php - SQL查询只显示第一个结果

c# - 在 Mono.Csharp 中运行小程序

wix - 如何将 64 位和 32 位 Windows Installer 程序包部署为单个设置?