php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30702 cannot initialize class variable from class constant
Submitted: 2004-11-06 15:30 UTC Modified: 2005-04-26 11:28 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: douglass_davis at earthlink dot net Assigned: andi (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-03-07 OS: *
Private report: No CVE-ID: None
 [2004-11-06 15:30 UTC] douglass_davis at earthlink dot net
Description:
------------
The manual says we can use constants in initializing class variables.  However, this is not the case, as you cannot initialize variables to constants that are defined in the same class.

When trying to compile it gives an error saying the class scope is not active.  However, the constant is inside the class.

Also, it does not give the error where the class is defined, but when it is instantiated.  So, perhaps it is an inlining error?

Maybe this is what's supposed to happen, if so, then the manual needs to say that constants from the same class can't be used to initialize a var.

Reproduce code:
---------------
// This works:

class Constants
{
  const DEFAULT_SIZE=5;
}

class Square
{
  public $size=Constants::DEFAULT_SIZE;
}
$r= new Square();

// this doesn't

class Square2
{
  const DEFAULT_SIZE=5;
  public $size=self::DEFAULT_SIZE;
}

$r= new Square2();

Expected result:
----------------
I expect it to compile

Actual result:
--------------
PHP Fatal error:  Cannot access self:: when no class scope is active in C:\src\test\consttest.php on line 23

Fatal error: Cannot access self:: when no class scope is active in C:\src\test\consttest.php on line 23


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-11 17:25 UTC] nlopess@php.net
I think this was supposed to work..
 [2004-11-16 12:22 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.0-win32-latest.zip

Works fine here.
 [2004-11-17 20:54 UTC] nlopess@php.net
It doesn't work with latest PHP 5.1 here:
Fatal error: Cannot access self:: when no class scope is active in C:\cygwin\home\Nuno\kk.php on line 21
 [2005-02-16 12:14 UTC] php at kaiundina dot de
seems to be the same Problem occuring at a slightly different place: Bug #31601
 [2005-03-07 21:49 UTC] sniper@php.net
Does not work with latest HEAD nor latest PHP_5_0

 [2005-04-26 11:28 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC