"Use of undefined constant" notice, but the constant should be defined
there are three files: common.php, control开发者_StackOverflow社区ler.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?
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.
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.
精彩评论