php require include
来源:互联网 发布:scp linux 端口 编辑:程序博客网 时间:2024/06/05 01:03
Its quite common to see such lines :
require_once('../../lib/some_class.php');
This approach has many drawbacks :
It first searches for directories specified in the include paths of php , then looks from the current directory.
So many directories are checked.
When a script is included by another script in a different directory , its base directory changes to that of the including script.
Another issue , is that when a script is being run from cron , it may not have its parent directory as the working directory.
So its a good idea to have absolute paths :
define('ROOT' , '/var/www/project/');require_once(ROOT . '../../lib/some_class.php');//rest of the code
Now this is an absolute path and will always stay constant. But we can improve this further. The directory /var/www/project can change , so do we change it everytime ? No instead we make it portable using magic constants like __FILE__ . Take a closer look :
//suppose your script is /var/www/project/index.php//Then __FILE__ will always have that full path.define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));require_once(ROOT . '../../lib/some_class.php');//rest of the code
So now even if you shift your project to a different directory , like moving it to an online server , the same code will run without any changes.
- php include VS require
- php:include&require
- php require include
- php--include and require
- PHP ----Include,require
- php require vs. include
- PHP-include和require
- PHP include和require
- php include和require
- PHP include 和 require
- PHP include vs require require vs require_once
- PHP---include、include_once、require、require_once
- PHP中include和require
- php require和include区别
- PHP中include和require
- php 中require() include() require_once()
- PHP的include和require
- PHP include 和require 语句
- 线段树
- 安徽省公安厅治安总队(通知)
- 关于GET和POST
- Orz教主 bjfu1151 字符串操作
- 河南省公安机关户政服务管理工作规范(试行)
- php require include
- 一个老中医的偏方(先保存,省的以后找~~)
- [转载]一篇好文章,关于职场,关于人生
- CentOS 6.3 VNC安装
- [IOS视图切换]ViewDeck类似path效果的实现
- ubuntu字典stardict安装——屏幕取词,好用
- Mac OS X 过往12年的演变史
- winsock
- SSE intrinsic函数_优化