Home php教程 php手册 PHP require/include顺序详解

PHP require/include顺序详解

May 25, 2016 pm 04:53 PM
include require order

在大型的web项目中, include_path是一个模块化设计的根本中的根本(当然,现在也有很多基于autoload的设计, 这个不影响本文的探讨), 但是正是因为include_path, 经常会让我们遇到一些因为没有找到正确的文件而导致的看似"诡异"的问题.也就有了如下的疑问:include_path是怎么起作用的?如果有多个include_path顺序是怎么样的?什么情况下include_path不起作用?

今天, 我就全面的介绍下这个问题, 先从一个例子开始吧.如下的目录结构:

1.php  2.php 3.php

3.php在1.php中:

<?php
ini_set("include_path", ".:path_to_subdir");  
require("2.php");  
?>
Copy after login

而在2.php中:

<?php
require("3.php");  
?>
Copy after login

而在root目录下的3.php打印出"root", 在subdir目录下的3.php打印出"subdir";现在, 我的问题来了:

1. 当在root目录下运行1.php, 会得到什么输出?

2. 在subdir下运行上一级目录的1.php, 有会得到什么输出?

3. 当取消include_path中的当前目录path(也就是include_path="path_to_subdir"), 上面俩个问题又会是什么输出?

php中的include_path

php在遇到require(_once)/include(_once)的指令的时候, 首先会做如下的判断:要包含的文件路径是绝对路径么? 如果是, 则直接包含, 并结束. 如果不是, 进入另外的逻辑(经过多次调用, 展开后进入_php_stream_fopen_with_path)寻找此文件接下来, 在_php_stream_fopen_with_path中, 会做如下判断:

要包含的文件路径是相对路径么(形如./file, ../dir/file, 以下用"目录相对路径代替")? 如果是, 则跳过include_path的作用逻辑, 直接解析相对路径(随后单独介绍)会根据include_path,和当前执行文件的path组成一个待选的目录列表, 比如对于文章前面的例子来说, 会形成一个如下的待选列表:path_to_subdir:current_script_dir然后, 依次从待选列表头部开始, 根据default_dir_separator(本文的环境是":")取出待选列表中的一个路径, 然后把要包含的文件名附加在这个路径后面, 进行尝试. 如果成功包含, 则返回, 否则继续下一个待选路径.

到现在为止, 我们已经可以回答我开头提出的3个问题了.

1. 因为在root目录下执行, 所以在1.php中包含2.php的时候, include_path的第二个待选路径起了作用(path_to_subdir), 找到了path_to_subdir/2.php, 而在2.php包含3.php的时候, 当前工作目录是root下, 所以在包含3.php的时候, include_path的第一个待选路径"."(当前工作目录)下就找到的匹配的文件, 所以得到的输出是"root".

2. 同1, 只不过当前的路径是subdir, 所以得到的输出是"subdir".

3. 因为没有了当前路径为include_path, 所以在root目录下运行的时候2.php中包含3.php的时候, 是path_to_subdir起了作用, 所以无论在root还是subdir都将得到"subdir"的输出. 而如果在2.php中清空include_path:

<?php
ini_set("include_path", &#39;&#39;);  
require("3.php");  
?>
Copy after login

那么将会是current_script_dir起作用, 而这个时候current_script_dir是2.php的路径, 所以还是会得到"subdir"的输出.

目录相对路径:在使用目录相对路径的情况下, 相对路径的基点, 永远都是当前工作目录,为了说明在目录相对路径下的情况,我们再看个列子, 还是上面的目录结构, 只不过1.php变成了:

<?php
ini_set("include_path", "/");  
require("./subdir/2.php");  
?>
Copy after login

2.php变成了:

<?php
require("./3.php");  
?>
Copy after login

如果在root目录下执行, 2.php中寻找3.php将会在当前目录的相对路径下寻找, 所以得到的输出是"root", 而如果是在subdir下执行上一级目录的1.php(php -f ../1.php), 将会因为在subdir下找不到"./subdir/2.php"而异常退出. 

后记:1. 因为使用include_path和相对路径的情况下, 性能会和寻找的次数有关, 最坏的情况下, 如果你有10个include_path, 那么最多可能会重试11次才能找到要包含的文件, 所以, 在能使用绝对路径的情况下最好使用绝对路径. 

2. 因为目录相对路径的basedir, 永远都是当前工作路径, 如果要使用, 需要和实际部署路径相关, 所以实际使用的很少(当然, 也有借助chdir来完成的模块). 

3. 在模块化的系统设计中, 一般应该在模块内, 通过获取模块的部署路径(dirname(__file__), php5.3以后更是提供了__dir__常量)从而使用绝对路径.


文章地址:

转载随意^^请带上本文地址!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

vue3+vite: How to solve the error when using require to dynamically import images in src vue3+vite: How to solve the error when using require to dynamically import images in src May 21, 2023 pm 03:16 PM

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

What is the difference between php include and include_once What is the difference between php include and include_once Mar 22, 2023 am 10:38 AM

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

What are the uses of require? What are the uses of require? Nov 27, 2023 am 10:03 AM

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages ​​or frameworks, require is used to perform specific tasks or functions.

Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Steps to solve the fatal error in the php header: require(): Failed opening required 'data/tdk.php' (include_path='.;C:\php\pear') Nov 27, 2023 pm 12:51 PM

Steps to resolve fatalerror:require():Failedopeningrequired'data/tdk.php'(include_path='.;C:phppear') in PHP header When developing websites or applications using PHP, we often encounter various errors . One of the common errors is "fatalerror:require():Failed

Order of printing numbers using thread synchronization Order of printing numbers using thread synchronization Sep 22, 2023 pm 09:41 PM

Here we will see how to print numbers in the correct order using different threads. Here we will create n threads and then synchronize them. The idea is that the first thread will print 1, then the second thread will print 2, and so on. When one thread tries to print, it locks the resource so other threads cannot use that part. Example #include<pthread.h>#include<stdio.h>#include<stdlib.h>#include<unistd.h>pthread_mutex_tmutex=PTHREAD_MUTEX_I

Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Steps to solve fatal error in php header: require(): Failed opening required 'data/tdk.php' Nov 27, 2023 am 10:41 AM

Steps to resolve FatalError:require():Failedopeningrequired'data/tdk.php' in PHP header When developing and maintaining PHP websites, we often encounter various errors and exceptions. One of the common errors is "FatalError:require():Failedopeningrequired'data/tdk.php'".

Detailed explanation of the role and usage of the require keyword in PHP Detailed explanation of the role and usage of the require keyword in PHP Jun 28, 2023 pm 11:31 PM

Detailed explanation of the role and usage of the require keyword in PHP In PHP development, require is a very commonly used keyword. Its function is to include the specified file for use by the current script. This article will explain in detail the function and use of the require keyword. 1. The role of the require keyword The require keyword can include the contents of a file into the current script. It is usually used to include some necessary external files, such as library files, configuration files, etc. Use req

PHP file:// PHP file:// Aug 20, 2023 pm 06:13 PM

Introduction In PHP, various URL style protocols can be used in conjunction with file system functions, with the help of corresponding built-in wrappers. Custom wrappers can also be defined using the stream_wrapper_register() function. The default wrapper in PHP is file://, which represents the local file system. If no other protocol is explicitly used, the PHP parser will treat it as a filesystem wrapper. The file name parameters passed to the file system functions fopen(), file_get_contents(), etc. use the file:// protocol by default. When the file name does not begin with a forward slash, a backslash, or the driver in Windows

See all articles