zoukankan      html  css  js  c++  java
  • PHP

    PHP - 获取和设置include_path              

    分类:             PHP             

    include_path是PHP中的一个环境变量,在php.ini中初始化设置,类似于JAVA的CLASSPATH和操作系统中的PATH。

    例如:有如下一些文件, /www/index.php /www/includes/config.php /www/includes/functions.php /www/includes/PEAR/PEAR.php /www/includes/PEAR/DB.php /www/includes/PEAR/DB/mysql.php

    如果没有设置include_path变量,index.php需要这样写:   

    1. <?php  
    2.   
    3. include_once '/www/includes/config.php';  
    4. include_once '/www/includes/PEAR/DB.php';  
    5. include_once '/www/includes/PEAR/DB/mysql.php';  
    6.   
    7. ?>  

    使用上面的引用方式,引用路径比较长,当引用目录位置改变时需要大量修改代码。使用include_path变量可以简化上述问题:

    1. <?php  
    2.   
    3. set_include_path(/www/includes' . PATH_SEPARATOR . /www/includes/PEAR');  
    4.   
    5. include_once 'config.php';  
    6. include_once 'DB.php';  
    7. include_once 'DB/mysql.php';  
    8.   
    9. ?>  

    include_path是PHP的环境变量,因而可以在php.ini设置,每次请求时include_path都会被PHP用php.ini中的值初始化。也可以用代码的方式修改include_path值,不过,修改后结果在请求完毕后会自动丢弃,不会带到下一个请求。因此,每次请求时需要重新设置。

    在代码中获取和设置include_path值有如下两种方式:

    方式一:ini_get()和ini_set()方式,此种方式使用于所有PHP版本。

    1. <?php  
    2.   
    3. $s = ini_get('include_path');  
    4.   
    5. ini_set($s . PATH_SEPARATOR . '/www/includes');  
    6.   
    7. ?>  

    方式二:get_include_path()和set_include_path()方式,此种方式使用于PHP4.3以后的版本。

    1. <?php  
    2.   
    3. $s = get_include_path();  
    4.   
    5. set_include_path($s . PATH_SEPARATOR . '/www/includes');  
    6.   
    7. ?>  

  • 相关阅读:
    在HTML中怎么去掉a标签(超链接)的下划线?
    鼠标移动到表格的TD上的时候显示成一个手型的样子怎么做?
    jackjson 为空的不参与序列号
    java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()
    mysql 索引
    jquery 中 attr 和 prop 区别
    mysql 5.7 版本 windows 安装
    mysql not in 和 not exits
    spring @Configuration
    spring boot 中 事务配置
  • 原文地址:https://www.cnblogs.com/archoncap/p/4534141.html
Copyright © 2011-2022 走看看