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?

11
  • 1
    are you sure it's indeed include('class.lib.php'); not something like include('http://example.com/class.lib.php'); ? Commented Jul 9, 2010 at 11:38
  • @Col. Shrapnel It isn't, but what difference could that make if it was? Would it cause the constants to not be defined? That doesn't seem to make sense. Commented Jul 9, 2010 at 12:09
  • Oh, but it does make sense. In the latter case you include the interpreted version of the script, which will not know of any constants you have defined when you include it, because it is run by itself in a different request (initiated from the server). Commented Jul 9, 2010 at 12:18
  • @wimvds So how would you make it run without it being a different request? There are other included files later in the script, which use various constants with no problems. How is it that they work whereas this one doesn't? Commented Jul 9, 2010 at 13:06
  • 1
    Can you try get_defined_constants() to see if the contents is consistent with your schema of the call stack?... Commented Jul 28, 2010 at 10:21

5 Answers 5

10
+50

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>
Sign up to request clarification or add additional context in comments.

4 Comments

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: Just out of curiosity, have you tried with the code that Artefacto posted? I am getting the same results as Artefacto in my environment.
@bcmcfc - so what is the difference between your code and @Artefacto's code? This suggests to me that Artefacto has answered your question.
I don't understand. What was the problem? How an answer which states the problem is not reproducible can be the solution?
-1

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');

1 Comment

As I said earlier, the constant is defined before it's used anywhere.
-1

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');

Comments

-1

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.

Comments

-2

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.

3 Comments

I see ;) But this error message is due to using non existing constants in situations where a string should be used.
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.
@bcmcfc: That's not true, I see one array: $_SERVER... ;) hahaha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.