PHP,如何设置包含路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6780236/
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-26 01:13:18  来源:igfitidea点击:

PHP, How to set include path

phppathinclude

提问by musicliftsme

I've been writing:

我一直在写:

include('a.php')
include('b.php')

etc. in my script to include to use functions a()and b(). It gets pretty tedious. Is there a way to set a path of a directory and have multiple files there be accessible by a script?

等在我的脚本中包含使用函数a()b(). 它变得非常乏味。有没有办法设置目录的路径并让脚本可以访问多个文件?

  1. I tried this in my script:

    set_include_path('.;C:\xampp\htdocs\myfunctionfolder');
    
  2. And I set an environmental variable PATH to have this older in there.

  3. I also in modified php.ini to include

    include_path = ".;\xampp\php\PEAR;\xampp\htdocs\myfunctionfolder"
    
  1. 我在我的脚本中试过这个:

    set_include_path('.;C:\xampp\htdocs\myfunctionfolder');
    
  2. 我设置了一个环境变量 PATH 让它在那里更旧。

  3. 我也在修改 php.ini 中包含

    include_path = ".;\xampp\php\PEAR;\xampp\htdocs\myfunctionfolder"
    

If I have many files in there, how do I access these files without having to include each individually? Setting the environmental variable definitely works in the command prompt.

如果我有很多文件,如何访问这些文件而不必单独包含每个文件?设置环境变量在命令提示符下肯定有效。

Do I need to do something else for .php files to be accessible collectively under a directory?

我是否需要为 .php 文件做其他事情才能在一个目录下集体访问?

回答by Brad Christie

Common practice is to have a "common.php" or "includes.php" file that includes the include/include_oncecalls (for the sake of simplicity). e.g.

通常的做法是拥有一个包含include/include_once调用的“common.php”或“includes.php”文件(为简单起见)。例如

  • root
  • [lib]
    • a.php
    • b.php
    • includes.php
  • index.php
  • [库]
    • 一个.php
    • b.php
    • 包含.php
  • 索引.php

Then includes.phpcontains:

然后includes.php包含:

<?php
  include_once('a.php');
  include_once('b.php');
?>

Then in any script it's a matter of including the includes.phpfile.

然后在任何脚本中都需要包含includes.php文件。

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendirand readdirto iterate over all files in a specific directory and include them as found (automated so-to-speak) or write out each include yourself based on the files you're creating.

但是,要回答您的原始问题,每次调用时您一次只能包含一个文件。您可以使用类似opendirreaddir来遍历特定目录中的所有文件,并将它们包含为找到的(自动这么说的)或根据您创建的文件自己写出每个包含。

Also, all setting the include path does is set a directory to look in when an includecall is made. It's not a directory where the files should automatically be loaded (which is the impression I get from your post).

此外,包含路径所做的所有设置都是在进行include调用时设置要查找的目录。它不是应该自动加载文件的目录(这是我从您的帖子中得到的印象)。

回答by Paul DelRe

Setting the include_pathwill not include every file in that directory, it only adds that directory to the list PHP will search when including a file.

设置include_path将不会包含该目录中的每个文件,它只会将该目录添加到 PHP 在包含文件时将搜索的列表中。

Specifies a list of directories where the require(), include(), fopen(), file(), readfile() and file_get_contents() functions look for files.

指定 require()、include()、fopen()、file()、readfile() 和 file_get_contents() 函数在其中查找文件的目录列表。

Source

来源

This would simplify including files in a deep structure or in a completely different section of the filesystem.

这将简化在深层结构或文件系统的完全不同部分中包含文件的过程。

include('/var/somewhere/else/foo.php');

With /var/somewhere/else/added to the php.ini include_path could become

随着/var/somewhere/else/加入到php.ini文件的include_path可能成为

include('foo.php');

Additionally, as others pointed out, there are common practices but you could look into OOPHP and autoloading classes. This will not work for functions that I know of.

此外,正如其他人指出的那样,有一些常见的做法,但您可以查看 OOPHP 和自动加载类。这不适用于我所知道的功能。

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).

In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

许多编写面向对象应用程序的开发人员为每个类定义创建一个 PHP 源文件。最大的烦恼之一是必须在每个脚本的开头编写一长串所需的包含(每个类一个)。

在 PHP 5 中,这不再是必要的。您可以定义一个 __autoload 函数,该函数会在您尝试使用尚未定义的类/接口时自动调用。通过调用这个函数,脚本引擎有最后的机会在 PHP 因错误而失败之前加载类。

回答by Evan

see this question:

看到这个问题:

How to include() all PHP files from a directory?

如何包含()目录中的所有 PHP 文件?

Also, in terms of best practices, you can include multiple functions in the same file if they are at all related, and I would also suggest having more descriptive names of your functions and files. For example, if your a() and b() functions both related to validation for example, name your file validation.php and put both functions in there and try to rename them to something that is related to what they do. This will allow you to remember what they do when you start piling up a huge list of functions ;)

此外,就最佳实践而言,您可以在同一个文件中包含多个函数(如果它们完全相关),并且我还建议您对函数和文件使用更具描述性的名称。例如,如果您的 a() 和 b() 函数都与验证相关,请将您的文件命名为 validation.php 并将这两个函数放在那里,并尝试将它们重命名为与它们所做的相关的内容。这将使您在开始堆积大量函数时记住它们的作用;)

回答by Marc B

PHP's parser is pretty efficient - you'll waste a lot more time loading a ton of individual files instead of one (or a few) more monolithic files. However, if you insist on keeping things segregated like that, you CAN create meta-include files to load sets of individual files, so you'd only include the one single meta-include file, and it does the rest for you:

PHP 的解析器非常高效 - 您将浪费更多时间加载大量单个文件而不是一个(或几个)更多的整体文件。但是,如果您坚持保持这样的隔离,您可以创建元包含文件来加载单个文件集,因此您只需包含一个元包含文件,其余的由它来为您完成:

meta.php:

元.php:

include('a.php');
include('p.php');
...
include('z.php');

And then you simply do:

然后你只需要做:

<?php

include('meta.php');

in your scripts and you've got all the individual ones loaded for you.

在您的脚本中,您已经为您加载了所有单独的脚本。

回答by Bob_Gneu

I have a function like this in most of my projects:

在我的大多数项目中,我都有这样的功能:

function appendToIncludePath($path)
{
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . BASE_DIR . $path . DIRECTORY_SEPARATOR);
}

回答by RENFiX

include __DIR__ . '/../folder1/folder2/folder3/Target.php';
include __DIR__ . '/../folder1/folder2/Target.php';

It helps you go to any path.

它可以帮助您走向任何道路。

回答by Vrushal Raut

Download latest PHP zip and extract to C drive then download composer and install it, during installation it ask for PHP path so just select extracted PHP path.

下载最新的PHP zip 并解压到C 盘,然后下载composer 并安装它,在安装过程中它会询问PHP 路径,因此只需选择解压的PHP 路径即可。

As follow below step.

按照下面的步骤。

  1. Go to Computer.

  2. Select Advance System Settings.

  3. From system properties select Environmental Varaibles.
  4. In Environmental Varaiblesadd Pathin User Variable for PCNAME
  5. In Environmental Varaiblesadd Pathin System Variables.
  6. Hit OK.
  7. Restart PC.
  8. Win + R type cmd.
  9. type command php -v.
  10. Vola you good to go.
  1. Computer

  2. 选择Advance System Settings

  3. 从系统属性中选择Environmental Varaibles
  4. Environmental Varaibles附加Path在用户变量的PCNAME
  5. 在系统变量中Environmental Varaibles添加Path
  6. 点击确定。
  7. 重启电脑。
  8. 赢+R型cmd
  9. 键入命令php -v
  10. 沃拉你很高兴去。

PHP http://php.net/downloads.php

PHP http://php.net/downloads.php

Composer https://getcomposer.org/download/

作曲家 https://getcomposer.org/download/