PHP,如何设置包含路径。

时间:2021-04-17 22:52:34

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

    我还用了修改过的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文件做其他事情,以便在目录下共同访问?

5 个解决方案

#1


11  

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

常见的做法是有一个“共同的”。php”或“包括。php“包含/include_once调用的文件(为了简单起见)。如。

  • root
  • [lib]
    • a.php
    • a.php
    • b.php
    • b.php
    • includes.php
    • includes.php
  • (*)。php b。php includes.php
  • index.php
  • index . php

Then includes.php contains:

然后包括。php包含:

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

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

然后在任何脚本中,包括包含在内。php文件。

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendir and readdir to 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.

但是,要回答您的原始问题,您只能每次调用一个文件。您可以使用opendir和readdir这样的工具来遍历特定目录中的所有文件,并将它们包含在您创建的文件的基础上(自动的so-to-speak)。

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

另外,所有设置include路径的设置都是在创建include调用时设置一个目录。它不是一个文件应该自动加载的目录(这是我从你的帖子得到的印象)。

#2


5  

Setting the include_path will 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.

指定需要()、include()、fopen()、文件()、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/ some/ 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和autoloading类。这对我所知道的函数不起作用。

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

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

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 5中,这不再是必需的。您可以定义一个__autoload函数,当您尝试使用尚未定义的类/接口时,它会自动调用。通过调用这个函数,脚本引擎将在PHP失败之前最后一次加载类。

#3


2  

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:

meta.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.

在你的脚本中,你已经为你加载了所有的个人信息。

#4


2  

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);
}

#5


2  

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()函数都与验证相关,请命名您的文件验证。把两个函数都放在这里,然后把它们重命名为与它们的功能相关的东西。这将让你记住当你开始堆积大量的函数时他们会做什么;

#1


11  

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

常见的做法是有一个“共同的”。php”或“包括。php“包含/include_once调用的文件(为了简单起见)。如。

  • root
  • [lib]
    • a.php
    • a.php
    • b.php
    • b.php
    • includes.php
    • includes.php
  • (*)。php b。php includes.php
  • index.php
  • index . php

Then includes.php contains:

然后包括。php包含:

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

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

然后在任何脚本中,包括包含在内。php文件。

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendir and readdir to 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.

但是,要回答您的原始问题,您只能每次调用一个文件。您可以使用opendir和readdir这样的工具来遍历特定目录中的所有文件,并将它们包含在您创建的文件的基础上(自动的so-to-speak)。

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

另外,所有设置include路径的设置都是在创建include调用时设置一个目录。它不是一个文件应该自动加载的目录(这是我从你的帖子得到的印象)。

#2


5  

Setting the include_path will 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.

指定需要()、include()、fopen()、文件()、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/ some/ 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和autoloading类。这对我所知道的函数不起作用。

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

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

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 5中,这不再是必需的。您可以定义一个__autoload函数,当您尝试使用尚未定义的类/接口时,它会自动调用。通过调用这个函数,脚本引擎将在PHP失败之前最后一次加载类。

#3


2  

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:

meta.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.

在你的脚本中,你已经为你加载了所有的个人信息。

#4


2  

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);
}

#5


2  

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()函数都与验证相关,请命名您的文件验证。把两个函数都放在这里,然后把它们重命名为与它们的功能相关的东西。这将让你记住当你开始堆积大量的函数时他们会做什么;