php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30619 class constants cannot be accessed from withing class static method
Submitted: 2004-10-30 07:00 UTC Modified: 2004-11-04 15:20 UTC
From: junk at pneyman dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.2 OS: Fedora Core 2, kernel 2.6.5
Private report: No CVE-ID: None
 [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.

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-04 10:36 UTC] 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-11-04 15:20 UTC] thekid@php.net
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"

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Aug 15 21:01:28 2024 UTC