一、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的更多相关文章

  1. php 中使用include、require、include_once、require_once的区别

    在PHP中,我们经常会通过include.require.include_once.require_once来引用文件,都可以达到引用文件的目的,但他们之间又有哪些区别呢,接一下我们详细的介绍一下 i ...

  2. 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 ...

  3. 【PHP】- include、require、include_once 和 require_once的区别

    1.include:会将指定的档案读入并且执行里面的程序.      被导入的档案中的程序代码都会被执行,而且这些程序在执行的时候会拥有和源文件中呼叫到 include() 函数的位置相同的变量范围( ...

  4. include、require、include_once和require_once的区别

    /*** * 1.include 和 require 的文件可以有返回值 * 2.include 包含的文件不存在,会发出一个警告,但是不会停止执行代码. * require 在这种情况下会抛出错误并 ...

  5. include、require、include_once和require_once理解

    都是在当前文件中包含引入并运行指定文件,include和require的不通之处仅仅在于发生错误时include产生一个警告脚本继续执行,而require产生一个致命的错误,脚本停止运行.有了once ...

  6. PHP中include、require、include_once、require_once的区别

    include:使用include引用外部文件时,只有代码执行到include代码段时,调用的外部文件才会被引用并读取,当引用的文件发生错误时,系统只会给出个警告错误,而整个php文件会继续执行.re ...

  7. PHP包含文件函数include、include_once、require、require_once区别总结

    一.使用语法和简介 1.include() 语法:include(/path/to/filename)include()语句将在其被调用的位置处包含一个文件.包含一个文件与在该语句所在位置复制制定文件 ...

  8. include()、include_once()与require()、require_once()的异同点

    相同点: 首先include().include_once()与require().require_once()都是用来包含并运行指定文件的,并且包含的文件在执行时在结构上是完全一样的. 例如:inc ...

  9. PHP中的include、include_once、require、require_once

    include.include_once().require.require_once() 作用: 通过 include 或 require 语句,可以将 PHP 文件的内容插入另一个 PHP 文件( ...

随机推荐

  1. Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting

    Convolutional LSTM Network: A Machine LearningApproach for Precipitation Nowcasting 这篇文章主要是了解方法. 原始文 ...

  2. 《笨方法学Python》加分题33

    while-leep 和我们接触过的 for-loop 类似,它们都会判断一个布尔表达式的真伪.也和 for 循环一样我们需要注意缩进,后续的练习会偏重这方面的练习.不同点在于 while 循环在执行 ...

  3. 人脸检测(1)——HOG特征

    一.概述 前面一个系列,我们对车牌识别的相关技术进行了研究,但是车牌识别相对来说还是比较简单的,后续本人会对人脸检测.人脸识别,人脸姿态估计和人眼识别做一定的学习和研究.其中人脸检测相对来说比较简单, ...

  4. <taglib>报错

    问题:web.xml中<taglib>报错 2.3版本可以直接卸载<web-app>中 2.4及之后放在<jsp-config>中 <jsp-config&g ...

  5. C# 结构体和List<T>类型数据转Json数据保存和读取

    C#  结构体和List<T>类型数据转Json数据保存和读取 一.结构体转Json public struct FaceLibrary { public string face_name ...

  6. shell脚本编写遍历某一目录下的所有文件

    遍历/root/321321/目录显示里面的所有文件 #!/bin/bash dir=`ls /root//` #定义遍历的目录 for i in $dir do echo $i done

  7. xpath json操作符说明

    XPath JSONPath Description / $ the root object/element . @ the current object/element / . or [] chil ...

  8. JPA-04

    一.JPQL JPA的查询语言(和SQL非常像,面向对象的查询语言) 有list集合可以用size看长度 分页:setFirstResult().setMaxResults(); 获取总条数:getS ...

  9. BERT(Bidirectional Encoder Representations from Transformers)

    BERT的新语言表示模型,它代表Transformer的双向编码器表示.与最近的其他语言表示模型不同,BERT旨在通过联合调节所有层中的上下文来预先训练深度双向表示.因此,预训练的BERT表示可以通过 ...

  10. mathematic语法基础

    1. 注释,用  (*这是注释*) 2.清除空间变量  Clear["`*"] 3. 求方程组.这个方程组比较特殊,有五个方程,六个变量,求其中五个变量(因变量)用另外一个变量(自 ...