php--include 、require
一、include 、require
定义:包含并运行指定文件
问题:查询了这两个语言结构的资料,有人说,什么require 先执行,什么include后执行.
思考:我觉得官方文档已经解释的很清楚了。这个两个参数的区别在于报错处理:
include 遇到错误会警告,程序继续.
require 遇到错误报错,程序结束.
由此可见,引申出了 "先执行后执行" 的问题:
假如,我在程序执行过程中需要加载一个文件,你说我用哪一个? -- 用 include , 程序执行过程中,有问题会报警但不会终止执行.
所以,我在程序运行时加载文件,用include。相反,如果我在程序开始时就加载文件,可以考虑用require.
二、require_once 和 include_once :
定义:相比于上面,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含.
这个就更好理解了,对于一个文件可能被加载多次,建议使用_once.
三、看看YII源码中的使用实例:
0、require
public function createController($route,$owner=null)
{
......
if(is_file($classFile))
{
if(!class_exists($className,false))
require($classFile);//这里
if(class_exists($className,false) && is_subclass_of($className,'CController'))
{
$id[0]=strtolower($id[0]);
return array(
new $className($controllerID.$id,$owner===$this?null:$owner),
$this->parseActionParams($route),
);
}
return null;
}
$controllerID.=$id;
$basePath.=DIRECTORY_SEPARATOR.$id;
}
}
这里使用了require,但是上下文中包含是否含有此文件判断,这个地方牵扯到创建controller,如此关键的步骤,需要让它有问题报错提示。
1、require_once
<?php // change the following paths if necessary
$yii=dirname(__FILE__).'/../../framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';
// remove the following line when in production mode
// defined('YII_DEBUG') or define('YII_DEBUG',true);
require_once($yii);
Yii::createWebApplication($config)->run();
这是某个apps下的入口文件,入口文件,会多次执行,所以只要验证加载了就不在加载。
2、include
1 public static function autoload($className,$classMapOnly=false)
2 {
3 // use include so that the error PHP file may appear
4 if(isset(self::$classMap[$className])) {
5
6 include(self::$classMap[$className]);
7 }
8 elseif(isset(self::$_coreClasses[$className]))
9 include(YII_PATH.self::$_coreClasses[$className]);
10 elseif($classMapOnly)
11 return false;
12 else
13 {
14 // include class file relying on include_path
15 .....
private static $_coreClasses=array(
'CApplication' => '/base/CApplication.php',
'CApplicationComponent' => '/base/CApplicationComponent.php',
'CBehavior' => '/base/CBehavior.php',
'CComponent' => '/base/CComponent.php', 'CCache' => '/caching/CCache.php',
截取一部分,这个autoload方法,根据参数classname作为key,从注册的_coreClasses提取value。
这里使用的是include,因为是在程序执行期间动态加载文件,所以使用了include.我想原因可能是加載的文件很多,不能保证所有文件不会变化,所以用到加载一遍,保证最新。
3、include_once
function highlight($str)
{
if (!($this->_renderer)) {
include_once(dirname(__FILE__).'/Renderer/Html.php');
$this->_renderer = new Text_Highlighter_Renderer_Html($this->_options);
}
$this->_state = -1;
$this->_pos = 0;
$this->_stack = array();
$this->_tokenStack = array();
$this->_lastinner = $this->_defClass;
$this->_lastdelim = $this->_defClass;
$this->_endpattern = '';
$this->_renderer->reset();
$this->_renderer->setCurrentLanguage($this->_language);
$this->_str = $this->_renderer->preprocess($str);
$this->_len = strlen($this->_str);
while ($token = $this->_getToken()) {
$this->_renderer->acceptToken($token[0], $token[1]);
}
$this->_renderer->finalize();
return $this->_renderer->getOutput();
}
这个高亮处理函数,就是采用了_once加载,目的也是运行中加载,并且只加载一次。
四、YII处理思路--动态加载
1、指定方法,注册到__autoload.
2、将可能用到的文件,以key-value形式存储为静态变量.
3、根据key(类名),对应出文件路径,使用 include 运行中加载.
总结:我觉得,决定怎样么用,需要结合项目,根据实际需要进行调用此语法结构。--以上如有错误请指出,我会及时更正,转php的道路上...
php--include 、require的更多相关文章
- php 中使用include、require、include_once、require_once的区别
在PHP中,我们经常会通过include.require.include_once.require_once来引用文件,都可以达到引用文件的目的,但他们之间又有哪些区别呢,接一下我们详细的介绍一下 i ...
- php-5.6.26源代码 - include_once、require_once、include、require、eval 的opcode处理器
# ZEND_INCLUDE_OR_EVAL_SPEC_CONST_HANDLER 实现在文件 php-\Zend\zend_vm_execute.h static int ZEND_FASTCALL ...
- 【PHP】- include、require、include_once 和 require_once的区别
1.include:会将指定的档案读入并且执行里面的程序. 被导入的档案中的程序代码都会被执行,而且这些程序在执行的时候会拥有和源文件中呼叫到 include() 函数的位置相同的变量范围( ...
- include、require、include_once和require_once的区别
/*** * 1.include 和 require 的文件可以有返回值 * 2.include 包含的文件不存在,会发出一个警告,但是不会停止执行代码. * require 在这种情况下会抛出错误并 ...
- include、require、include_once和require_once理解
都是在当前文件中包含引入并运行指定文件,include和require的不通之处仅仅在于发生错误时include产生一个警告脚本继续执行,而require产生一个致命的错误,脚本停止运行.有了once ...
- PHP中include、require、include_once、require_once的区别
include:使用include引用外部文件时,只有代码执行到include代码段时,调用的外部文件才会被引用并读取,当引用的文件发生错误时,系统只会给出个警告错误,而整个php文件会继续执行.re ...
- PHP包含文件函数include、include_once、require、require_once区别总结
一.使用语法和简介 1.include() 语法:include(/path/to/filename)include()语句将在其被调用的位置处包含一个文件.包含一个文件与在该语句所在位置复制制定文件 ...
- include()、include_once()与require()、require_once()的异同点
相同点: 首先include().include_once()与require().require_once()都是用来包含并运行指定文件的,并且包含的文件在执行时在结构上是完全一样的. 例如:inc ...
- PHP中的include、include_once、require、require_once
include.include_once().require.require_once() 作用: 通过 include 或 require 语句,可以将 PHP 文件的内容插入另一个 PHP 文件( ...
随机推荐
- clean-room 洁净室软件工程
众所周知,软件工程的主要目的是提高软件的开发效率和软件质量.近年来发展起来的洁净室软件工程(cleanroom software engineering)提出了用统计的质量控制方法管理软件 ...
- if(变量)的判断
变量如果不为0,null,undefined,false,都会被处理为true.只要变量有非0的值或是某个对象,数组,字符串,都会认为true
- bittorrent 学习(三) MSG
msg.c中 int转化 char[4] char[4]转化int的函数 如下(有多种方案) ]) { c[] = i % ; c[] = (i - c[]) / % ; c[] = (i - c[ ...
- python 子进程 subpocess 的使用方法简单介绍
python的子进程嘛,就是利用python打开一个子进程(当然像是一句废话),但是可能和我们理解的不太一样. 一:如何理解? 我们可能的理解:多开一个进程运行某个python函数(如果只想实现这个功 ...
- dubbo-2.5.6优雅停机研究
不优雅的停机: 当进程存在正在运行的线程时,如果直接执行kill -9 pid时,那么这个正在执行的线程被中断,就好像一个机器运行中突然遭遇断电的情况,所导致的结果是造成服务调用的消费端报错,也有可能 ...
- ubuntu16.04安装tensorflow1.3
总结 : 1.点软件个更新-系统更新2.降级gcc到5.33.装CUDA及第二个包,加入PATH4.CUDNN5.Ancada..6.TF Ubuntu16.04 的GCC版本降级 http://bl ...
- 解决vs验证控件报错” WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping”问题
将RequiredFieldValidator的 EnableClientScript属性设置成 False 适用于大多验证控件
- pycharm汉化(3.6版本)
step 1:下载pycharm汉化包 链接:https://pan.baidu.com/s/1htgcbZY 密码:8uia step 2:将pycharm安装目录下的lib文件夹内下的resou ...
- nginx server
配置nginx 首先apt install nginx 然后安装php apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-mbs ...
- java 幂等性(转)
(转自)http://www.cnblogs.com/weidagang2046/archive/2011/06/04/idempotence.html 理解HTTP幂等性 基于HTTP协议的Web ...