我玩了我的PHP脚本,一旦我对include进行了修改,执行时间减少了25%。
我的旧包括:
include_once "somefile.php";
我的新人是:
include './somefile.php';
因此,我想尝试将所有include_once转换为包含,如果我不小心,我可能会不必要地两次包含相同的文件。
我有文件设置,以便它们代表如下所示:
index2.php file: file user accesses:
<?php
//index2.php
include "../relative/path/to/includes/main.php";
anythingelse();
exit();
?>
index.php file: file user accesses:
<?php
//index.php
include "../relative/path/to/includes/main.php";
anything();
exit();
?>
core.php file: Loads every other file if not loaded so all functions work
<?php
include_once "../relative/path/to/includes/db.php";
include_once "../relative/path/to/includes/util.php";
include_once "../relative/path/to/includes/logs.php";
//core - main.php
function anything(){
initdb();
loaddb();
getsomething();
makealog();
}
function anythingelse(){
initdb();
loaddb();
getsomething();
getsomething();
makealog();
}
?>
db.php file: helper file
<?php
//database - db.php
function initdb(){
//initialize db
}
function loaddb(){
//load db
}
?>
util.php file: helper file
<?php
//utilities - util.php
function getsomething(){
//get something
}
?>
logs.php file: helper file
<?php
//logging - logs.php
function makealog(){
//generate a log
}
?>
我的设置的想法是index.php和index2.php是用户可以直接访问的文件。核心文件是所有功能的根源,因为它加载了剩余的php文件,其中包含核心文件使用所需的功能,然后由index.php和index2.php文件使用。
目前,解决这个问题的方法是让我替换:
include_once "../relative/path/to/includes/db.php";
include_once "../relative/path/to/includes/util.php";
include_once "../relative/path/to/includes/logs.php";
什么都没有,在index2.php和index.php中,我在include下添加这些行:
include '/absolute/path/to/includes/db.php';
include '/absolute/path/to/includes/util.php';
include '/absolute/path/to/includes/logs.php';
问题是,我有几十个文件,我必须这样做,我想知道是否有另一种解决方法,而不是将所有功能合并到一个文件中,因为实际上,每个PHP文件我都是包含至少1,000行代码(有些行包含至少3行命令)。我正在寻找一种能够缩短执行时间的解决方案。