PHP constants are undefined - but they are defined!

I use constants to set various configuration variables in a script.

The INC_PATH constant is defined in a script that includes a class library.

define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');

The class library contains various include lines ('someClass.php'). It also contains:

require(INC_PATH.'DB.class.php');

The class library issues a notification:

Use of undefined constant INC_PATH - assumed 'INC_PATH'

How can the class library not see that the constant INC_PATH is defined? I thought constants are global?

+3
source share
5 answers

Yes, but they must be defined before:

<?php
echo INC_PATH; //undefined
define('INC_PATH', "foo");
echo INC_PATH; //defined

In response to your comment

I can not reproduce this:

a.php

<?php
define('INC_PATH',$_SERVER['DOCUMENT_ROOT']."/");
include('b.php.inc');

b.php.inc

<h1><?php require(INC_PATH . "c.php.inc"); ?></h1>

c.php.inc

<?php echo INC_PATH; ?>

The request a.phpgives:

<h1>U:/htdocs/</h1>
+8
source

( ): ,

include('class.lib.php');

sometime INC_PATH.


, :

throw new Exception('err'); //-- Verify the stack trace !
require(INC_PATH.'DB.class.php');
-1

, define() , , , if()?

, INC_PATH , class.lib.php?

...
...
var_dump(INC_PATH);exit;
include('class.lib.php');
-1

,

define('INC_PATH',$_SERVER['DOCUMENT_ROOT'].'includes/');
include('class.lib.php');

.


, , , , .

, ,

<?php
  $var = false;
  // This if block will not execute
  if($var) // because if($var) will be executed as if(false)
  {
    define('TEST_CONST', 'This is a test');
  }
  echo TEST_CONST;
?>

:

Notice: Use of undefined constant TEST_CONST - assumed 'TEST_CONST' in C:\www\test2.php on line 7
TEST_CONST

, .

-1

, ? :

$array[value]
// instead of:
$array['value']

Added single quotes in your error message are an indicator, quotes will not be added to the constant.

-2
source

Source: https://habr.com/ru/post/1753869/


All Articles