It's very confusing that you dont get a warning (whats the errorlevel?), because if define really does not work for constants inside the class object, you *should* get first a notice "Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in ...".
Try to find out if this is the case.
I played around a little and it depends on WHEN a new class object is created. The problem ist NOT the declaration of the class, but the moment, where you need the constants. Two different scripts, the first runs fine, the second does not. I think, it's not a bug, it works as designed:
No Error Script:
- Code: Select all
<?php
define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
$a = new MySQL();
class MySQL {
function MySQL() {
mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
}
}
?>
Error Script:
- Code: Select all
<?php
$a = new MySQL();
define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
class MySQL {
function MySQL() {
mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
}
}
?>
Another No Error Script:
- Code: Select all
<?php
class MySQL {
function MySQL() {
mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
}
}
define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
$a = new MySQL();
?>
Another Error Script:
- Code: Select all
<?php
class MySQL {
function MySQL() {
mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
}
}
$a = new MySQL();
define("DB_SERVER","localhost");
define("DB_USER","user");
define("DB_PASS","password");
define("DB_NAME","database");
?>
You see, everything depends on WHEN you create a new instance of your object. It does NOT depend on WHEN you DECLARE the class. The declaration of a Constant via define() MUST be before you use that constant. A class declaration does not use the Constant, it is only used, when you create an instance of this class.
This is definately quite logical and i think it's ok, as a call to define() is a normal function call and as PHP is a procedural language, the resultung constant does NOT exist before that call.
Maybe it worked different in older versions of PHP, but PHP5 has a complete new redesign of object classes and i think, it's more logical now.
Greets
Scory