php set_include_path(get_include_path() .PATH_SEPARATOR .'phpseclib');

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14295164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:01:15  来源:igfitidea点击:

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

phpftpsftpphpseclib

提问by John

I have stumbled upon two functions i have never used before in php

我偶然发现了两个我以前从未在 php 中使用过的函数

set_include_path();
get_include_path();

I am currently looking to implement the phpseclib onto a project I am working on.. As i need to use the SFTP class extension of the SSH2 which in turn requires the MathBigInteger class.. etc etc.

我目前正在寻找将 phpseclib 实施到我正在处理的项目中。因为我需要使用 SSH2 的 SFTP 类扩展,而后者又需要 MathBigInteger 类......等等。

The manual says about set_include_path():

手册上说set_include_path()

"Sets the include_path configuration option for the duration of the script. "

“为脚本的持续时间设置 include_path 配置选项。”

What does this mean for the rest of my framework, will it set ALL include paths from the 'phpseclib' dir?

这对我框架的其余部分意味着什么,它会设置来自“phpseclib”目录的所有包含路径吗?

Also, I really don't get:

另外,我真的不明白:

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

I am storing the php sec in a custom library directory in my file system, does get_include_path() some how magically find the phpseclib directory in my filesystem?

我将 php sec 存储在我的文件系统中的自定义库目录中,get_include_path() 是否在我的文件系统中神奇地找到了 phpseclib 目录?

As you can see I am completely lost here.. could anyone be kind enough to shed some light for me please?

正如你所看到的,我在这里完全迷失了......有人可以为我提供一些帮助吗?

PS/ I only need this library at one partivular point in the application thus only want to include it when needed, at present I am wanting to include it within a child of my model class.

PS/ 我只需要在应用程序的一个特定点上使用这个库,因此只想在需要时包含它,目前我想将它包含在我的模型类的子类中。

回答by Jon

First of all you need to understand what the include_pathconfiguration setting does:

首先,您需要了解include_path配置设置的作用:

Specifies a list of directories where the require, include, fopen(), file(), readfile() and file_get_contents() functions look for files. The format is like the system's PATH environment variable: a list of directories separated with a colon in Unix or semicolon in Windows.

PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error. You may modify or set your include path at runtime using set_include_path().

指定 require、include、fopen()、file()、readfile() 和 file_get_contents() 函数在其中查找文件的目录列表。格式类似于系统的 PATH 环境变量:在 Unix 中用冒号或在 Windows 中用分号分隔的目录列表。

PHP 在查找要包含的文件时分别考虑包含路径中的每个条目。它将检查第一个路径,如果没有找到,则检查下一个路径,直到它找到包含的文件或返回警告或错误。您可以在运行时使用 set_include_path() 修改或设置包含路径。

The construct

构造

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

appends phpseclibto the list of directories searched when you request including a file with one of the above functions.

phpseclib当您请求包含具有上述功能之一的文件时,将附加到搜索的目录列表中。

Since phpseclibis a relative path, the effect is the same as if you had specified ./phpseclib, i.e. PHP will look into a subdirectory named phpseclibinside the current directory of the process. It does not magically determine where the library is located in the filesystem; it's your job to put it where it will be found.

由于phpseclib是相对路径,效果和你指定的一样./phpseclib,即PHP会查找phpseclib进程当前目录下的子目录。它不会神奇地确定库在文件系统中的位置;把它放在可以找到的地方是你的工作。

回答by lafor

To better understand what include_pathis and isn't, read this sectionof php.ini manual.

为了更好地理解什么include_path是什么不是,请阅读php.ini 手册的这一部分

If you're trying to write a project-specific "autoloader", set_include_pathis not the best tool for that (you might want to look into spl_autoload_registerinstead), but to answer your question:

如果您正在尝试编写特定于项目的“自动加载器”,那么set_include_path这不是最好的工具(您可能想要研究一下spl_autoload_register),而是回答您的问题:

set_include_pathdoes overwrite whatever previous include_pathwas. Multiple paths can be provided using PATH_SEPARATORconstant as a separator, eg:

set_include_path确实会覆盖以前的内容include_path。可以使用PATH_SEPARATOR常量作为分隔符提供多个路径,例如:

set_include_path($path1. PATH_SEPARATOR . $path2 . PATH_SEPARATOR . $path3);

thus you can add to already existing path instead of overwriting it like this:

因此你可以添加到已经存在的路径而不是像这样覆盖它:

set_include_path(get_include_path() . PATH_SEPARATOR . $mypath);

回答by John

The the set_include_path just sets a possible location for the PHP engine to look for files. For example:

set_include_path 只是为 PHP 引擎设置一个可能的位置来查找文件。例如:

set_include_path( WEBROOT_PRIVATE.'scripts\phpseclib' );
if(include('Net/SSH2.php')){
    echo 'pass';
} else {
    echo 'fail';
}
include WEBROOT_PRIVATE.'application/global_function_list.php';

The above worked perfectly well and continues to include correctly other files required for the application.

以上工作得很好,并继续正确包含应用程序所需的其他文件。

回答by Rory Larson

John, I think the reason your second example fails is that 'phpseclib' doesn't specify a full path. Assuming your first example works, you would want to replace

约翰,我认为您的第二个示例失败的原因是“phpseclib”没有指定完整路径。假设你的第一个例子有效,你会想要替换

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

with

set_include_path(get_include_path() . PATH_SEPARATOR . WEBROOT_PRIVATE.'scripts\phpseclib');

in the second example. To confirm that your include_path is what you think it should be, you might add the line

在第二个例子中。要确认您的 include_path 是您认为应该的样子,您可以添加以下行

echo get_include_path() . " is my include path.\n";

right after the set_include_path() call.

在 set_include_path() 调用之后。