Home >Backend Development >PHP Tutorial >PHP automatically loads classes and sets the include directory. It doesn't go wrong with anything new_PHP Tutorial

PHP automatically loads classes and sets the include directory. It doesn't go wrong with anything new_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:46961browse

get_include_path — Get the current include_path configuration option

string get_include_path (void)


set_include_path — Set include_path configuration option
string set_include_path ( string $new_include_path )


First, 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 following include and require path ranges, or predefined.
Like:
If we do not set this value, we may need to write some complete paths:


[php]
include("123/test1.php");
include("123/test2.php");
include("123/test3.php");
require("123/test4.php");
require("123/test5.php");
?>

                                                         include("123/test1.php");
             include("123/test2.php");
             include("123/test3.php");
            require("123/test4.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.

[php]
set_include_path("123/");
include("test1.php");
include("test2.php");
include("test3.php");
require("test4.php");
require("test5.php");
?>

                                                        set_include_path("123/");

             include("test1.php");
             include("test2.php");
             include("test3.php");
           require("test4.php");
            require("test5.php");
​ ​ ?> ​ Then this function can not only define one folder, we can define many folders. As shown below, I want to write an initialization function:
[php]
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/");

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/");
} So its path becomes:

.;C:php5pear;core/;app/;admin/;lib/;include/;data/;cache/

Here’s an example.

[php]
$include_path=get_include_path();                                                                                                                                                                                                                                             $include_path.=PATH_SEPARATOR."include/" ;
$include_path.=PATH_SEPARATOR."classs/";
$include_path.=PATH_SEPARATOR."libs/";
//echo $include_path;
//Set all directories where include files are located
set_include_path($include_path);

function __autoload($className)
{
//echo 'Class'.$className;
include strtolower($className).".class.php";
}
$Smarty = new Smarty;
?>

$include_path=get_include_path();                                                                                                                                                                                                                                                   $include_path.=PATH_SEPARATOR."include/" ;
$include_path.=PATH_SEPARATOR."classs/";
$include_path.=PATH_SEPARATOR."libs/";
//echo $include_path;
//Set all directories where include files are located
set_include_path($include_path);

function __autoload($className)

{

//echo 'Class'.$className;
include strtolower($className).".class.php";
}
$Smarty = new Smarty;
?>

When multiple directories are specified as include_path, and the files required to be included have files with the same name in these directories, PHP chooses to use the file in the directory with the highest ranking when include_path is set.


This way you can directly pull new!!

http://www.bkjia.com/PHPjc/477451.html

truehttp: //www.bkjia.com/PHPjc/477451.htmlTechArticleget_include_path Get the current include_path configuration option string get_include_path ( void ) set_include_path Set include_path configuration option string set_include_path ( string $new_. ..
Statement:
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