set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
Asked Answered
G

4

12

I have stumbled upon two functions i have never used before in 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.

The manual says about set_include_path():

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

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

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?

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.

Glint answered 12/1, 2013 at 16:8 Comment(0)
B
17

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

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().

The construct

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

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

Since phpseclib is a relative path, the effect is the same as if you had specified ./phpseclib, i.e. PHP will look into a subdirectory named phpseclib inside 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.

Bacterin answered 12/1, 2013 at 16:32 Comment(1)
thank you, this was a completely new concept to me, but makes much more sense nowGlint
O
6

To better understand what include_path is and isn't, read this section of php.ini manual.

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

set_include_path does overwrite whatever previous include_path was. Multiple paths can be provided using PATH_SEPARATOR constant as a separator, eg:

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);
Overglaze answered 12/1, 2013 at 16:34 Comment(1)
Thank you. Php... never ending!Glint
G
1

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

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.

Glint answered 12/1, 2013 at 16:31 Comment(0)
E
0

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

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

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

right after the set_include_path() call.

Essex answered 4/3, 2014 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.