php ABSPATH 还是 __FILE__?

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

ABSPATH or __FILE__?

phpwordpress

提问by Scott B

Can someone tell me if either of these two methods has an advantage over the other and why?

有人能告诉我这两种方法中的任何一种是否比另一种有优势,为什么?

$mydir = ABSPATH.'/wp-content/themes/mytheme/images';

$mydir = ABSPATH.'/wp-content/themes/mytheme/images';

$mydir = dirname(__FILE__).'/images';

$mydir = dirname(__FILE__).'/images';

They can both be used to obtain and absolute path to the images directory of "mytheme" regardless of structure of whether wordpress is installed on the root directory or in a subdirectory off the root. In both cases, they are being called from the functions.php file which is located under the "mytheme" folder.

无论wordpress是安装在根目录还是根目录下的子目录中,它们都可以用于获取“mytheme”图像目录的绝对路径。在这两种情况下,它们都是从位于“mytheme”文件夹下的functions.php 文件中调用的。

回答by Pekka

I would personally prefer dirname()as it is always guaranteed to give me the correct result, while the ABSPATH method relies on a fixed theme path and theme name that can both change.

我个人更喜欢dirname()它,因为它总是保证给我正确的结果,而 ABSPATH 方法依赖于一个固定的主题路径和主题名称,它们都可以改变。

By the way, you can use __DIR__instead of dirname(__FILE__).

顺便说一句,您可以使用__DIR__代替dirname(__FILE__).

回答by Adam

回答by Alix Axel

For my own projects I would choose dirname(__FILE__), also there is a new constant in PHP:

对于我自己的项目,我会选择dirname(__FILE__),PHP 中还有一个新常量:

__DIR__ === dirname(__FILE__)

回答by ravs21292

ABSPATH is defined variable -> define("ABSPATH",__FILE__); if i directly use magic constant __FILE__.it will produce same result.

ABSPATH 是定义变量 ->define("ABSPATH", __FILE__); 如果我直接使用魔术常量。__FILE__它会产生相同的结果。

In CMS ABSPATH and framework use BASEPATH is used to get root information in the form of defined variable . In the end with the help of both we get same accurate result.

在 CMS ABSPATH 和框架中使用 BASEPATH 以定义变量的形式获取根信息。最后在两者的帮助下,我们得到了同样准确的结果。