|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-10-30 07:00 UTC] junk at pneyman dot com
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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 04:00:02 2025 UTC |
The line private static $currentDb = PostgresDb; should read private static $currentDb = self::PostgresDb; Example: $ cat test.php <?php class Foo { const PostgresDb = 'PostgreSQL'; private static $dbname= self::PostgresDb; public static function test() { var_dump(self::PostgresDb, self::$dbname); } } Foo::test(); ?> $ php5 test.php string(10) "PostgreSQL" string(10) "PostgreSQL"