• php--include 、require


    一、include 、require  

       定义:包含并运行指定文件

       问题:查询了这两个语言结构的资料,有人说,什么require  先执行,什么include后执行.

       思考:我觉得官方文档已经解释的很清楚了。这个两个参数的区别在于报错处理:

          include 遇到错误会警告,程序继续.

          require 遇到错误报错,程序结束.

      由此可见,引申出了 "先执行后执行" 的问题:

        假如,我在程序执行过程中需要加载一个文件,你说我用哪一个? -- 用 include , 程序执行过程中,有问题会报警但不会终止执行.

        所以,我在程序运行时加载文件,用include。相反,如果我在程序开始时就加载文件,可以考虑用require.

    二、require_once  和 include_once :

      定义:相比于上面,唯一区别是 PHP 会检查该文件是否已经被包含过,如果是则不会再次包含.

            这个就更好理解了,对于一个文件可能被加载多次,建议使用_once.

    三、看看YII源码中的使用实例:

    0、require

     1 public function createController($route,$owner=null)
     2     {
     3             ......
    42             if(is_file($classFile))
    43             {
    44                 if(!class_exists($className,false))
    45                     require($classFile);//这里
    46                 if(class_exists($className,false) && is_subclass_of($className,'CController'))
    47                 {
    48                     $id[0]=strtolower($id[0]);
    49                     return array(
    50                         new $className($controllerID.$id,$owner===$this?null:$owner),
    51                         $this->parseActionParams($route),
    52                     );
    53                 }
    54                 return null;
    55             }
    56             $controllerID.=$id;
    57             $basePath.=DIRECTORY_SEPARATOR.$id;
    58         }
    59     }

    这里使用了require,但是上下文中包含是否含有此文件判断,这个地方牵扯到创建controller,如此关键的步骤,需要让它有问题报错提示。

    1、require_once  

    1 <?php
    2 
    3 // change the following paths if necessary
    4 $yii=dirname(__FILE__).'/../../framework/yii.php';
    5 $config=dirname(__FILE__).'/protected/config/main.php';
    6 // remove the following line when in production mode
    7 // defined('YII_DEBUG') or define('YII_DEBUG',true);
    8 require_once($yii);
    9 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         .....
    1 private static $_coreClasses=array(
    2         'CApplication' => '/base/CApplication.php',
    3         'CApplicationComponent' => '/base/CApplicationComponent.php',
    4         'CBehavior' => '/base/CBehavior.php',
    5         'CComponent' => '/base/CComponent.php',
    6 
    7         'CCache' => '/caching/CCache.php',

    截取一部分,这个autoload方法,根据参数classname作为key,从注册的_coreClasses提取value。

    这里使用的是include,因为是在程序执行期间动态加载文件,所以使用了include.我想原因可能是加載的文件很多,不能保证所有文件不会变化,所以用到加载一遍,保证最新。

    3、include_once

     1 function highlight($str)
     2     {
     3         if (!($this->_renderer)) {
     4             include_once(dirname(__FILE__).'/Renderer/Html.php');
     5             $this->_renderer = new Text_Highlighter_Renderer_Html($this->_options);
     6         }
     7         $this->_state = -1;
     8         $this->_pos = 0;
     9         $this->_stack = array();
    10         $this->_tokenStack = array();
    11         $this->_lastinner = $this->_defClass;
    12         $this->_lastdelim = $this->_defClass;
    13         $this->_endpattern = '';
    14         $this->_renderer->reset();
    15         $this->_renderer->setCurrentLanguage($this->_language);
    16         $this->_str = $this->_renderer->preprocess($str);
    17         $this->_len = strlen($this->_str);
    18         while ($token = $this->_getToken()) {
    19             $this->_renderer->acceptToken($token[0], $token[1]);
    20         }
    21         $this->_renderer->finalize();
    22         return $this->_renderer->getOutput();
    23     }

    这个高亮处理函数,就是采用了_once加载,目的也是运行中加载,并且只加载一次。

     四、YII处理思路--动态加载

      1、指定方法,注册到__autoload.

      2、将可能用到的文件,以key-value形式存储为静态变量.

      3、根据key(类名),对应出文件路径,使用 include 运行中加载.  

    总结:我觉得,决定怎样么用,需要结合项目,根据实际需要进行调用此语法结构。--以上如有错误请指出,我会及时更正,转php的道路上...
  • 相关阅读:
    turtle库笔记
    使用turtle库绘制一个红色五角星图形‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪
    postgresql更新sequence的起始值
    es启动报错-系统设置
    mybatis批量update
    postgresql数据库连接数查询
    org.postgresql.util.PSQLException: 栏位索引超过许可范围:1,栏位数:0。
    postgresql创建SEQUENCE
    unzip解压所有zip格式
    jdk8中map的merge方法介绍
  • 原文地址:https://www.cnblogs.com/sunchong/p/7060798.html
Copyright © 2020-2023  润新知