3

there are three files: common.php, controller.php and user.php.

File common.php looks like:

<?php
define("MAXIMUM_NAME_LENGTH", 50);
?>

File controller.php looks like:

<?php
include("common.php");
include("user.php");
$user = new user();
?>

File user.php looks like:

<?php
class user{
private $var = MAXIMUM_NAME_LENGTH;
}
?>

When executing script there is given notice: Notice: Use of undefined constant MAXIMUM_NAME_LENGTH - assumed 'MAXIMUM_NAME_LENGTH' in /.../controller.php on line xxx. I want to share defined values in common.php between other files. How to do it in a proper way?

scdmb
  • 15,091
  • 21
  • 85
  • 128
  • is your `user.php` file altering in any way the `MAXIMUM_NAME_LENGTH` var? – gion_13 May 11 '11 at 20:50
  • 1
    change your `include` to `require` to make sure your files are actually included correctly. A quick test with PHP 5.2 confirms that this exact code should work. – Marek Karbarz May 11 '11 at 20:57
  • try to replace `include` by `require` - maybe file is not included (wrong path). – OZ_ May 11 '11 at 20:59

2 Answers2

1

Generally you would have all defines included by the file that is going to use them. IE: In your example, make the class file have a $class->setMaxNameLength(); method and pass the name length define in through there, that way it won't throw you an error.

cbroughton
  • 1,726
  • 1
  • 12
  • 19
1

Place error_reporting(E_ALL); the line before include("common.php");.

Most probably you'll see something like:

Warning: include(common.php) [function.include]: failed to open stream: No such file or directory in xxx

indicating that your three files are not in the same folder together, or together in a path that is not in your include_path, or a combination of that.

EDIT:

If you are using Suhosin, check your suhosin.executor.include.* settings.

Jürgen Thelen
  • 12,745
  • 7
  • 52
  • 71