#30619 [Bgs]: class constants cannot be accessed from withing class static method
| From: | thekid@php.net | Date: | Thu, 04 Nov 2004 14:20:21 +0000 |
| Subject: | #30619 [Bgs]: class constants cannot be accessed from withing class static method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-68695@lists.php.net to get a copy of this message | ||
ID: 30619
Updated by: thekid@php.net
Reported By: junk at pneyman dot com
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Fedora Core 2, kernel 2.6.5
PHP Version: 5.0.2
New Comment:
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"
Previous Comments:
------------------------------------------------------------------------
[2004-11-04 10:36:29] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions.
Thank you for your interest in PHP.
change
.. private static $currentDb = PostgresDb; ...
to
.. private static $currentDb = 'PostgresDb'; ...
------------------------------------------------------------------------
[2004-10-30 07:00:33] 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.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30619&edit=1