PHP 路径的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10424963/
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
Best practice for PHP paths
提问by Nath5
I have been all over the Internet trying to figure out the best way to handle paths in my website. Should I use relative paths, absolute paths?
我一直在互联网上试图找出处理我网站中路径的最佳方法。我应该使用相对路径还是绝对路径?
I have seen dirname (FILE) mentioned several times. One problem I have with relative paths is that PHP files which are included by several other files at different directory levels cause the relative paths to break. For example if the directory structure is
我已经看到多次提到dirname ( FILE)。我在使用相对路径时遇到的一个问题是,包含在不同目录级别的其他几个文件中的 PHP 文件会导致相对路径中断。例如,如果目录结构是
Root
A
B
b
And a PHP file in b and An include another file from B then the relative paths for the code in the file in B will be different.
而 b 中的 PHP 文件和 An 包含来自 B 的另一个文件,那么 B 中文件中代码的相对路径将不同。
So in general what is the best way to handle paths to files with regards to includes and file operations within the code.
所以一般来说,处理代码中包含和文件操作的文件路径的最佳方法是什么。
回答by Lawrence Cherone
Tho there are many ways to find out the path I always find it easiest to define a constant within a file on the root of the project index.phpor a configof sort.
then I can use SITE_ROOTfor includes/class loaders ect, and SITE_URLfor views, controllers, redirects ect.
虽然有很多方法可以找到路径,但我总是发现在项目根目录index.php或config某种类型的文件中定义常量是最容易的。然后我可以SITE_ROOT用于包含/类加载器等,以及SITE_URL视图、控制器、重定向等。
<?php
$root=pathinfo($_SERVER['SCRIPT_FILENAME']);
define ('BASE_FOLDER', basename($root['dirname']));
define ('SITE_ROOT', realpath(dirname(__FILE__)));
define ('SITE_URL', 'http://'.$_SERVER['HTTP_HOST'].'/'.BASE_FOLDER);
?>
<?php
function __autoload($class_name) {
include (SITE_ROOT.'/includes/'.$class_name.'.php');
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
回答by TheCheese
The Zend framework has some good pointers about optimizing include paths:
Zend 框架有一些关于优化包含路径的好建议:
http://framework.zend.com/manual/1.10/en/performance.classloading.html
http://framework.zend.com/manual/1.10/en/performance.classloading.html
Even if you're not using the Zend framework, these are good pointers. The general bulletpoints are:
即使您没有使用 Zend 框架,这些也是很好的指针。一般要点是:
- Use absolute paths
- Reduce the number of include paths you define
- Define the current directory last, or not at all
- Define your Zend Framework include_path as early as possible (not really relevant if you don't use Zend)
- 使用绝对路径
- 减少您定义的包含路径的数量
- 最后定义当前目录,或者根本不定义
- 尽早定义您的 Zend 框架 include_path (如果您不使用 Zend,则不太相关)
回答by Ahatius
You could put all your include files into one main directory. Then create a path variable in a config file or in the script itself, which points to the include directory.
您可以将所有包含文件放在一个主目录中。然后在配置文件或脚本本身中创建一个指向包含目录的路径变量。
回答by Pete
The best way to use proper naming conventions for the Directory Structure, PHP files and Class names of the file and the design a autoloader to include the file
为文件的目录结构、PHP 文件和类名使用正确命名约定并设计自动加载器以包含文件的最佳方法
回答by bitoshi.n
New version of php (PHP5.3) is possible to use __autoloadSo, you just need to determine root of your application.
新版本的 php (PHP5.3) 可以使用__autoload因此,您只需要确定应用程序的根目录。

