set_include_path usage

WBOY
Release: 2016-07-29 09:10:28
Original
1101 people have browsed it

Reprinted from: http://blog.sina.com.cn/s/blog_4ce89f200100twbl.html

http://blog.sina.com.cn/s/blog_815611fb0101cqgy.html

When I was studying the source code in the company today, I saw a line of code like set_include_path(dirname(__FILE__)); and I was very puzzled. So I searched on Baidu and looked at the php.net manual, but I still didn't quite understand what it did.
I accidentally saw another piece of code, and then I got inspired and tested it, and it was as expected!
Now let me summarize:
First of all, the set_include_path function dynamically modifies the include_path in PHP.ini in the script.
As for this include_path, it can be limited to the path range of the include andrequirebelow, or it can be predefined.
is like:
If we don’t set this value, maybe we need to write some complete paths:

include("123/test1.php");
include("123/test2.php");
require("123/test5.php");
?>
to introduce many external files, but if we set set_include_path("123/"), we can use the following code instead.

set_include_path("123/"); include("test2.php");
    include( "test3.php");Because, when executed When include orrequire
operates, it will go to the path specified by include_path to find the file to be imported. Although I don't know if this will optimize performance, it is certain that it can save some code. Haha~
So at the beginning, I thought it would be the same whether I added it or not, because I only included one file in this folder.
Later, I finally discovered the mystery! What’s deplorable is that there isn’t an article like mine on the Internet…the same article is reposted over and over again.
Then this function can not only define one folder, we can define many folders. As shown below, I want to write an initialization function:
function initialize(){
set_include_path(get_include_path().PATH_SEPARATOR . "core/");
set_include_path(get_include_path ().PATH_SEPARATOR . "app/");
set_include_path(get_include_path().PATH_SEPARATOR . "admin/");set_include_path(get_include_path().PATH_SEPARATOR . "lib/");set_include_path( get_include_path().PATH_SEPARATOR . "include/");
set_include_path(get_include_path().PATH_SEPARATOR."data/");
set_include_path(get_include_path().PATH_SEPARATOR."cache/");
}
This way it The path becomes:
.;C:php5pear;core/;app/;admin/;lib/;include/;data/;cache/
Hey, we found that there is another .;C: php5pear; What is going on? In fact, if we directly output the default value of include_path without writing anything, we will find that it is...; C: php5pear; It allows you to access any imported file.

If many more folders are loaded, we can just write the file name directly!
But just like my original question, why is our company’s code different from other people’s? It turns out that if I just write a
use ’s ’ using ’s ’ using ’s ’ ‐ ‐ ‐‐ ‐‐ ‐ toThe file in the folder will report an error saying that it cannot be found in the folder I specified.
First, let’s use another method to output it:

set_include_path(dirname(__FILE__));
$inc lude_value = ini_get('include_path');
echo $include_value; include("test4.php" ; opening 'test1.php' for inclusion (include_path='D:AppServwww') in
D:AppServwwwtest.phpon line6
And we also found that.;C:php5pear; has been replaced. So when we use it, if we don’t just introduce files in one folder, we need to add get_include_path().PATH_SEPARATOR in front of it.值
PATH_SEPARATOR 是个常量,是include的路径分界符合,在window上是;在unix和Linux上是:最后,我还要说一下,其实我们也可以通过另外一种方法:即最Original:

ini_set
('include_path'
,
'directory name'
);In addition, two points to note are:If in the specified
The requested file cannot be found in the directory, and when a file with this name happens to exist in the current page directory, the file in the current directory will be imported by default.

The above introduces the usage of set_include_path, including the require content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!