|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-11-11 17:25 UTC] nlopess@php.net
[2004-11-16 12:22 UTC] tony2001@php.net
[2004-11-17 20:54 UTC] nlopess@php.net
[2005-02-16 12:14 UTC] php at kaiundina dot de
[2005-03-07 21:49 UTC] sniper@php.net
[2005-04-26 11:28 UTC] dmitry@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 01:00:01 2025 UTC |
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