PHP set_inlude_path并包含最佳实践

时间:2021-09-28 22:51:49

Where would

set_include_path("../../");

point to?

I am trying to run some PHP code on a standard XAMPP server. And put my stuff into the htdocs folder. The includes point to relative paths, But it does not work. Is there any best-practise for includes? The code has to run on machines of multiple developers.

我试图在标准的XAMPP服务器上运行一些PHP代码。并将我的东西放入htdocs文件夹。包含指向相对路径,但它不起作用。是否包含最佳实践?代码必须在多个开发人员的机器上运行。

2 个解决方案

#1


Relative path should work fine (warning: apache on windows, as I know, don't follow the simlinks, or whatever are called on that os).

相对路径应该工作正常(警告:Windows上的apache,据我所知,不遵循simlinks,或者在那个操作系统上调用的任何东西)。

Maybe you should use the syntax (in order to avoid problem with different php versions)

也许你应该使用语法(为了避免不同的PHP版本的问题)

ini_set('include_path', 'yourdir');

and test the return value to see if all is ok.

并测试返回值以查看是否一切正常。

Turning to the best practices: To me setting an configuration directive into a script, expecially if project-wide, is wrong, or at least dangerous.

转向最佳实践:对我来说,将配置指令设置为脚本,特别是在项目范围内,是错误的,或者至少是危险的。

A better practice is to put the directive into the .htaccess file in the directory that contains your php files project.

更好的做法是将指令放入包含php文件项目的目录中的.htaccess文件中。

Even better, it is faster, put the directives into the appropriate virtualhost section of your apache config. For develop, though, .htaccess is more flexible and therefore preferable.

更好的是,它更快,将指令放入apache配置的相应虚拟主机部分。但是,对于开发来说,.htaccess更灵活,因此更受欢迎。

It should be something like this: php_value include_path ".:../..:<your path collection>"

它应该是这样的:php_value include_path“。:../ ..: <你的路径集合> ”

Doing this lets you share the php configuration w/o have fiddling with the ini_set directive in every php file you write.

这样做可以让你共享php配置,而不是在你编写的每个php文件中摆弄ini_set指令。

Not to mention that if you have a special file that needs a custom include_path, you can set it in the file and this will be evident to everyone at a very rapid glance.

更不用说如果你有一个需要自定义include_path的特殊文件,你可以在文件中设置它,这对每个人来说都很明显。

#2


Two directories down from where the file (or the file which included it) is running from.

从文件(或包含它的文件)运行的两个目录。

As for best practices there are only two advices i can give you.

至于最佳实践,我只能给你两个建议。

  1. Use relative paths.
  2. 使用相对路径。

  3. If relative paths are giving you trouble, use an absolute path as a single static variable and the developers only change that one variable.
  4. 如果相对路径给您带来麻烦,请使用绝对路径作为单个静态变量,开发人员只更改该变量。

#1


Relative path should work fine (warning: apache on windows, as I know, don't follow the simlinks, or whatever are called on that os).

相对路径应该工作正常(警告:Windows上的apache,据我所知,不遵循simlinks,或者在那个操作系统上调用的任何东西)。

Maybe you should use the syntax (in order to avoid problem with different php versions)

也许你应该使用语法(为了避免不同的PHP版本的问题)

ini_set('include_path', 'yourdir');

and test the return value to see if all is ok.

并测试返回值以查看是否一切正常。

Turning to the best practices: To me setting an configuration directive into a script, expecially if project-wide, is wrong, or at least dangerous.

转向最佳实践:对我来说,将配置指令设置为脚本,特别是在项目范围内,是错误的,或者至少是危险的。

A better practice is to put the directive into the .htaccess file in the directory that contains your php files project.

更好的做法是将指令放入包含php文件项目的目录中的.htaccess文件中。

Even better, it is faster, put the directives into the appropriate virtualhost section of your apache config. For develop, though, .htaccess is more flexible and therefore preferable.

更好的是,它更快,将指令放入apache配置的相应虚拟主机部分。但是,对于开发来说,.htaccess更灵活,因此更受欢迎。

It should be something like this: php_value include_path ".:../..:<your path collection>"

它应该是这样的:php_value include_path“。:../ ..: <你的路径集合> ”

Doing this lets you share the php configuration w/o have fiddling with the ini_set directive in every php file you write.

这样做可以让你共享php配置,而不是在你编写的每个php文件中摆弄ini_set指令。

Not to mention that if you have a special file that needs a custom include_path, you can set it in the file and this will be evident to everyone at a very rapid glance.

更不用说如果你有一个需要自定义include_path的特殊文件,你可以在文件中设置它,这对每个人来说都很明显。

#2


Two directories down from where the file (or the file which included it) is running from.

从文件(或包含它的文件)运行的两个目录。

As for best practices there are only two advices i can give you.

至于最佳实践,我只能给你两个建议。

  1. Use relative paths.
  2. 使用相对路径。

  3. If relative paths are giving you trouble, use an absolute path as a single static variable and the developers only change that one variable.
  4. 如果相对路径给您带来麻烦,请使用绝对路径作为单个静态变量,开发人员只更改该变量。