From: junk at pneyman dot com
Operating system: Fedora Core 2, kernel 2.6.5
PHP version: 5.0.2
PHP Bug Type: Scripting Engine problem
Bug description: class constants cannot be accessed from withing class static method
Description:
------------
Seems that there's no way to address the class constant from within a
static method declared in the same class. Script is parsed by the engine,
but I get a warning notice: Use of undefined constant 'xxx', assuming
'xxx'. The name of the constant assumed is identical to the one I want to
address.
The substitution then occurs correctly, and script works. However, I
couldn't find a way to call cosntant correctly to avoid warning message
and guesswork from the engine.
self:: resolution operator doesn't help either.
Reproduce code:
---------------
include_once("db/PostGreSqlDbProfile.php");
include_once("db/MySqlDbProfile.php");
class InitStingray {
// constants
const PostgresDb = 'PostGreSQL';
const MysqlDb = 'MySQL';
// private variables
private static $currentDb = PostgresDb;
private static $db = NULL;
// public methods
private static function initDb () {
// xxx: problem addressing constant PostgresDb
if (self::$currentDb == self::PostgresDb) {
self::$db = new PostGreSqlDbProfile();
}
// xxx: problem addressing constant MySqlDb
else if (self::$currentDb = self::MysqlDb) {
self::$db = new MySqlDbProfile();
}
}
public static function getDb () {
if (self::$db == NULL) {
self::initDb();
return self::$db;
}
else {
return self::$db;
}
}
}
Expected result:
----------------
Substitution of the name of the constant for its value should occur, and
comparisons between variable $currentDb and predefined values "Postgres"
and "Mysql" should take place. No warnings should be issued.
Actual result:
--------------
Warning is issued that the script cannot locate the constant I'm
addressing, however guesses that I'm trying to call a cosntant with the
same name and correctly does the substitution issuing a warning notice.
--
Edit bug report at http://bugs.php.net/?id=30619&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=30619&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=30619&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=30619&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=30619&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=30619&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=30619&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=30619&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=30619&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=30619&r=support
Expected behavior: http://bugs.php.net/fix.php?id=30619&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=30619&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=30619&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=30619&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=30619&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=30619&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=30619&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=30619&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=30619&r=float
MySQL Configuration Error: http://bugs.php.net/fix.php?id=30619&r=mysqlcfg