开发者

Constant defined by PHP define() really global?

开发者 https://www.devze.com 2023-03-04 10:25 出处:网络
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 chec

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?

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

/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

<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,

if index开发者_如何学运维.php call it, the

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

if a1.php call it, the

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

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

therefore, we need to add 3 path.php in this 3 directroy, as follow,

/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

 <?

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

?>

/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

<?

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

/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,

if caller is /index.php, then ROOT_PATH = "FULLPATH", so

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

if caller is /ajax/a1.php, then ROOT_PATH = "..", so

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 included or required inside it.

It's not magically available in other, unrelated scripts on your webserver.


Update

So you're trying to obtain the path to your webroot from any arbitrary file underneath it.

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.

You could try this:

In utility.php:

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

In somedir/somefile.php:

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


Constant are superglobals, once defined , you can use them without using the keyword global to import it into the current scope.

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

  • http://www.php.net/define
  • http://www.php.net/manual/en/language.constants.php
0

精彩评论

暂无评论...
验证码 换一张
取 消