tags:

views:

201

answers:

5

I'm using constants to set various configuration variables within a script.

The INC_PATH constant is defined within the script that includes the class library.

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

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

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

The class library throws a notice:

Use of undefined constant INC_PATH - assumed 'INC_PATH'

How is the class library not able to see that the INC_PATH constant has been defined? I thought constants were global?

+7  A: 

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't reproduce that:

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; ?>

Asking for a.php gives:

<h1>U:/htdocs/</h1>
Artefacto
It is defined before - apologies if the initial post appeared misleading- the first line of code in the post is inside the class.lib.php file which is included after the INC_PATH is defined.
bcmcfc
@bcm I can't reproduce (see edit). I'm using PHP 5.3.0.
Artefacto
@bcmcfc: Just out of curiosity, have you tried with the code that Artefacto posted? I am getting the same results as Artefacto in my environment.
pferate
@pferate I can't reproduce the error with Artefacto's code.
bcmcfc
@bcmcfc - so what is the difference between your code and @Artefacto's code? This suggests to me that Artefacto has answered your question.
Sohnee
I don't understand. What was the problem? How an answer which states the problem is not reproducible can be the solution?
pascal
A: 

Are you sure this is exactly the problem? This error message is moytly displayed on accessing array values with no quotes for the keys:

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

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

Tobias P.
There's not an array in sight...
bcmcfc
I see ;) But this error message is due to using non existing constants in situations where a string should be used.
Tobias P.
@Tobias: The posted error does indeed come from an undefined constant, but not always from forgetting quotes for a string. Constants are very useful if you aren't familiar with them.
pferate
@bcmcfc: That's not true, I see one array: $_SERVER... ;) hahaha.
pferate
A: 

A wild guess (worth checking though) : This notice could arise if your line

include('class.lib.php');

is called sometime before the declaration of the INC_PATH constant.


Modify your class library file and verify who actually includes the file :

throw new Exception('err'); //-- Verify the stack trace !
require(INC_PATH.'DB.class.php');
JonG
As I said earlier, the constant is defined before it's used anywhere.
bcmcfc
A: 

Is it possible that the define() called is within a block of code that isn't being executed, like an if() statement?

What do you get if you verify the value of INC_PATH immediately before you include class.lib.php?

...
...
var_dump(INC_PATH);exit;
include('class.lib.php');
pferate
A: 

Probably you write the code

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

within a block.


If yo write your code within any block, if the block is not executed then the variable or constants declared in the block will not been initialized.

Suppose, You make a code like this,

<?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;
?>

It will give a output like that:

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

Correct this to solve your problem.

chanchal1987