没有合适的资源?快使用搜索试试~ 我知道了~
PHP程序员最常用的两个函数莫过于require_once和include了,通过这两个函数,我们可以使用其他类库中定义的类等对象。但很多人在使用包含相同目录下的其他文件时,仅仅简单使用下面的代码进行文件引用
资源推荐
资源详情
资源评论
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/release/download_crawler_static/13062781/bg1.jpg)
php中中inlcude()性能对比详解性能对比详解
PHP程序员最常用的两个函数莫过于require_once和include了,通过这两个函数,我们可以使用其他类库中定
义的类等对象。但很多人在使用包含相同目录下的其他文件时,仅仅简单使用下面的代码进行文件引用
include性能性能
复制代码 代码如下:
include('include.php');
当然这种方式并没有错误,只不过在效率上它比下面的方式要稍稍差些:
复制代码 代码如下:
include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.'include.php');
这种方式我们可能需要输入更多一些,但相对于前面那种需要PHP引擎去include_path 中迭代查找所有名称为'include.php'才
能查找到相应对象来说,dirname(__FILE__)这种绝对路径的指定会让系统迅速定位到相应文件。
在PHP中常量__FILE__ 其实跟C#中的AppDomain.CurrentDomain.BaseDirectory非常类似,它返回的是当前代码正在执行的
代码所在文件的绝对路径。而函数dirname()则返回其父文件夹路径。
另外一个更查找效率高,并且书写简单的方式是include('./include.php'),这相当于告诉系统查找当前路径下的'include.php'文
件。
在大型系统中我们常常使用另外一个更好的方式,我们常常在路由文件或其他初始化文件中加入如下代码:
复制代码 代码如下:
define('APP_PATH',realpath(dirname(_FILE_)));
这相当于给系统添加了一个全局变量来指出系统根目录,当我们后面需要引用某具体路径下的文件时我们就可以使用如下代码
了:
复制代码 代码如下:
include(APP_PATH.DIRECTORY_SEPARATOR.'models'.'User.php');
autoload与与include性能比较性能比较
例如有以下四个脚本:
复制代码 代码如下:
#file:include1.php
include 'include2.php';
//@todo something#file:include2.php
//@todo something#file:script1.php
include 'include2.php';
//@todo something
#file:script2.php
include 'include1.php';
include 'script1.php'
//@todo something
当执行script1.php时, include ‘include2.php'; 这行代码被执行了一次。而执行script2.php时,这行代码被执行了两次。
这里只是一个简单的例子,在实际的项目中,include2.php被include的次数可能更多。这样反复的include,是否会影响性能
呢?为此我写了个脚本来测试。
复制代码 代码如下:
#file:SimpleClass.php
class SimpleClass {
public function __construct() {
echo get_time() . "rn";
}
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38723699
- 粉丝: 6
- 资源: 871
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 打包和分发Rust工具.pdf
- SQL中的CREATE LOGFILE GROUP 语句.pdf
- C语言-leetcode题解之第172题阶乘后的零.zip
- C语言-leetcode题解之第171题Excel列表序号.zip
- C语言-leetcode题解之第169题多数元素.zip
- ocr-图像识别资源ocr-图像识别资源
- 图像识别:基于Resnet50 + VGG16模型融合的人体细胞癌症分类模型实现-图像识别资源
- C语言-leetcode题解之第168题Excel列表名称.zip
- C语言-leetcode题解之第167题两数之和II-输入有序数组.zip
- C语言-leetcode题解之第166题分数到小数.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)