简体   繁体   English

为什么在PHP中使用set_include_path()以及如何从不同的文件夹中自动加载类?

[英]Why use set_include_path() in PHP and how to autoload classes from different folders?

I have a couple questions about the include path in PHP and including files and/or classes. 我有几个关于PHP中包含路径的问题,包括文件和/或类。

Below is a simple snippet of code where we are setting multiple include paths. 下面是一个简单的代码片段,我们在其中设置多个包含路径。 I saw this in another project but I am not sure why? 我在另一个项目中看到了这个,但我不确定为什么?

I have never had to use set_include_path() in any of my projects over the last 5 years or so. 在过去5年左右的时间里,我从未在任何项目中使用过set_include_path()

Q1) Q1)
So what exactly is the purpose of setting an include path? 那么设置包含路径的目的到底是什么? I have always just included the path in my include() call. 我总是在include()调用中包含路径。

Q2) Q2)
In the example below it sets more then 1 path. 在下面的示例中,它设置了多于1个路径。 How does this work for including files in multiple locations, i'm confused on the purpose or what it does exactly? 如何将文件包含在多个位置,我对目的或准确的做法感到困惑?

<?php
// Define App path
define('APPLICATION_PATH', realpath('../'));

// Build array of 3 different paths
$paths = array(
    APPLICATION_PATH,
    APPLICATION_PATH . '\com',
    get_include_path()
);


/*
Result of array above...
Array
(
    [0] => E:\Web Server\xampp\htdocs\test
    [1] => E:\Web Server\xampp\htdocs\test\com
    [2] => .;C:\php5\pear
)
*/

// Set include path from array above
// http://us3.php.net/manual/en/function.set-include-path.php
set_include_path(implode(PATH_SEPARATOR, $paths));

?>

Q3) Q3)
This is slightly different question but still relates to includes. 这是一个略有不同的问题,但仍然涉及到包括。 Below is a simple autoload function for classes. 下面是一个简单的类自动加载功能。 I used to have a classes folder and autoload ALL my class files. 我曾经有一个类文件夹和自动加载所有我的类文件。 In my current project, I have a library of classes to autoload like below does, but I then also have another section where I might need to autoload class files from a modules directory. 在我当前的项目中,我有一个类自动加载的类库,如下所示,但我还有另一个部分,我可能需要从modules目录自动加载类文件。

So I will need to autoload my library classes located somewhere like this.... 所以我需要自动加载位于这样的地方的库类 ....

root/includes/library/classes/library_class_files.php 根/包括/库/类/ library_class_files.php

+++plus+++ +++加+++

load classes for different modules (account, messages, friends, photos,blogs, forums, etc) located somewhere like this.... 加载位于这样的地方的不同模块(帐户,消息,朋友,照片,博客,论坛等)的类。

root/modules/forums/modules_class_files.php 根/模块/论坛/ modules_class_files.php

I might not need to loads class files from the 2 different locations, but if I do, how would I go about doing that? 我可能不需要从2个不同的位置加载类文件,但如果我这样做,我将如何去做呢?

<?php
//auto include class files that we need on a per page basis
function __autoload($class_name){
    include('library/classes/' .$class_name . '.class.php');
}
?>

Q1: http://php.net/manual/en/ini.core.php#ini.include-path Q1: http//php.net/manual/en/ini.core.php#ini.include-path

Q2: As mentioned in the manual PHP iterates over every path and tries to find your file. Q2:如手册中所述,PHP遍历每个路径并尝试查找您的文件。

Q3: Using the more modern SPL Autoloader functionality you can define as many autoloaders as you like. 问题3:使用更现代的SPL Autoloader功能,您可以根据需要定义任意数量的自动加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM