简体   繁体   English

PHP定义的常量define()真的是全局的吗?

[英]Constant defined by PHP define() really global?

I just define a root path as a constant like this 我只是将根路径定义为这样的常量

define("ROOT_PATH", dirname(__FILE__));

so, at the same file, they can recognize ROOT_PATH, however, in the other file, ROOT_PATH is null, I check some forums, people said defined constant can be use in global, however, I can't, is that I need to do some config in PHP.ini? 因此,在同一文件中,他们可以识别ROOT_PATH,但是,在另一个文件中,ROOT_PATH为null,我检查了一些论坛,人们说已定义的常量可以在全局中使用,但是,我不能,是我需要在PHP.ini中做一些配置?

Actually, why I do that is because I'm moving the site to new server, however, the new server seems don't like me use relative path, therefore, I need to change to absolute path. 实际上,为什么要这样做是因为我要将站点移至新服务器,但是,新服务器似乎不喜欢我使用相对路径,因此,我需要更改为绝对路径。

UPDATE: 更新:

Thanks everyone, I think I catch the reason, I use ajax to call the file, so the ROOT_PATH is null, therefore, I define ROOT_PATH in every file, but another problem is appear, my file structure is sth like this 谢谢大家,我想我明白了原因,我使用ajax来调用文件,因此ROOT_PATH为null,因此,我在每个文件中都定义了ROOT_PATH,但是出现了另一个问题,我的文件结构是这样的

/index.php
/ajax/a1.php
/inc/inc.php

index.php:
<?
define('ROOT_PATH',dirname(__FILE__));
include_once ROOT_PATH."/inc/inc.php";
?>


a1.php:
<?
define('ROOT_PATH',dirname(__FILE__));
include_once ROOT_PATH."/inc/inc.php";
?>

so, when the client side call a1.php, it will include inc.php, however, it doesn't like what I imagine, I get this from the chrome developer tools 因此,当客户端调用a1.php时,它将包含inc.php,但是,它不符合我的想象,我是从chrome开发人员工具中获得的

<b>Warning</b>:  include_once(D:\.............\ajax/inc/a1.php) [<a href='function.include-once'>function.include-once</a>]: failed to open stream: No such file or directory in <b>D:\..................\inc\inc.php</b> on line <b>3</b><br />

I check with the ROOT_PATH generated by both file, find that, FILE is depends on the caller, 我检查了两个文件生成的ROOT_PATH,发现FILE是取决于调用者的,

if index.php call it, the 如果index.php调用它,

ROOT_PATH = d:\.............\

if a1.php call it, the 如果a1.php调用它,

ROOT_PATH = d:\..............\ajax\

however, both file I need to call that file, any method can I overcome this? 但是,两个文件都需要调用该文件,任何方法都可以克服吗? thanks. 谢谢。

SOLVED: 解决了:

Hi, I just figure out how to over come the path problem, first of all, we need to setup a file in every directory, and this file store the absolute path information refer to the relative path(hard to understand? :P), I call this file path.php , for my example 嗨,我只是想出了解决路径问题的方法,首先,我们需要在每个目录中设置一个文件,该文件存储的绝对路径信息是指相对路径(很难理解吗?:P),我将这个文件称为path.php

/index.php
/ajax/a1.php
/inc/inc.php

therefore, we need to add 3 path.php in this 3 directroy, as follow, 因此,我们需要在这3个Directroy中添加3个path.php,如下所示,

/index.php
/path.php
/ajax/a1.php
/ajax/path.php
/inc/inc.php
/inc/path.php

each setting of the path.php like this, 像这样设置path.php每个设置,

/path.php

 <?

if(!defined(ROOT_PATH))
    define(ROOT_PATH, dirname(__FILE__));

?>

/ajax/path.php & /inc/path.php /ajax/path.php/inc/path.php

<?

if(!defined(ROOT_PATH))
    define(ROOT_PATH, '..');

?>

so, if we have a 3 leve structure, let say: /one/two/ , then the /one/two/path.php should be like this 因此,如果我们有一个3级结构,可以说: /one/two/ ,那么/one/two/path.php应该像这样

<?

if(!defined(ROOT_PATH))
    define(ROOT_PATH, '../..');

?>

After we set up all this file, we need to include this path.php as follow, for example with index.php 设置完所有文件后,我们需要按如下方式包含该path.php ,例如index.php

/index.php

<?

include_once "path.php";
include_once ROOT_PATH."/inc/inc.php";

...
?>

So, whoever the caller is, it will first include the local path.php , and find out the relative path (/ or .. or ../..) , therefore, the included file will not be wrong because all of them are include with correct symbol, let say, 因此,无论调用者是谁,它都将首先包含本地path.php ,并找出相对路径(/ or .. or ../..) ,因此,包含的文件不会出错,因为它们都是加上正确的符号,然后说,

if caller is /index.php, then ROOT_PATH = "FULLPATH", so 如果调用者是/index.php,则ROOT_PATH =“ FULLPATH”,因此

include_once ROOT_PATH."/inc/inc.php";
=
include_once "FULLPATH"."/inc/inc.php";

if caller is /ajax/a1.php, then ROOT_PATH = "..", so 如果调用者是/ajax/a1.php,则ROOT_PATH =“ ..”,因此

include_once ROOT_PATH."/inc/inc.php";
=
include_once ".."."/inc/inc.php";

I think you're misunderstanding what "global" means. 我认为您误解了“全球”的含义。

A constant is available in a script, from the point you define it until the end of the script, including in the context of any scripts include d or require d inside it. 从定义变量到脚本结束,脚本中都可以使用常量,包括在脚本中include d或require d的上下文中。

It's not magically available in other, unrelated scripts on your webserver. 它在Web服务器上的其他不相关脚本中无法正常使用。


Update 更新资料

So you're trying to obtain the path to your webroot from any arbitrary file underneath it. 因此,您正在尝试从其下的任意文件获取Webroot的路径。

Of course __FILE__ won't work, as an individual file knows only its own path, and not how far down the directory structure it is. 当然__FILE__行不通的,因为单个文件仅知道其自己的路径,而不知道目录结构的向下位置。

You could try this: 您可以尝试以下方法:

In utility.php : utility.php

define('ROOT_PATH', dirname(__FILE__));

In somedir/somefile.php : somedir/somefile.php

require_once "../utility.php";
echo ROOT_PATH;

Constant are superglobals, once define d , you can use them without using the keyword global to import it into the current scope. 常量是超全局变量,一旦定义 d,就可以使用它们而无需使用关键字global将其导入当前范围。

Once you define a constant it is available into the current file (from the definition onward) and into the required/included files that are included after the constant definition. 定义常数后,它就可用于当前文件(从定义开始)以及常数定义之后包含的必需/包含的文件中。

This is all. 这就是全部。 You can't access to the constant into a different file if the file is not included into the defining one or if the defining file is not included into the file you you need to access the constant. 如果该文件未包含在定义文件中,或者如果定义文件未包含在文件中,则您将无法访问该常量到另一个文件中。

Such defines really are global. 这样的定义确实是全球性的。 You are probably using ROOT_PATH before you are defining it. 在定义它之前,您可能正在使用ROOT_PATH Turn on error reporting and check the order of your includes. 打开错误报告并检查包含顺序。

The primary definition of define() and other pages does not tell about constants being scoped at any way: define()和其他页面的主要定义不能以任何方式说明常量的范围:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM