php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31601 static variable-initialisation varies depending on construction scope
Submitted: 2005-01-19 00:19 UTC Modified: 2006-02-28 23:24 UTC
Votes:4
Avg. Score:4.2 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: php at kaiundina dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.0.2 OS: Win XP prof/Apache 2
Private report: No CVE-ID:
 [2005-01-19 00:19 UTC] php at kaiundina dot de
Description:
------------
I have the following problem using the 'self'-operator for static variable initialisation:

I created a class ('Item' in the reproduce code) persisting of the following elements:
- class-constants containing key-strings
- a static variable containing an array using the above constants as associative keys. The array was preinitialized with some values. As nonation for the keys I used 'self::CONST_NAME' ('self::ARRAY_KEY' in the example)

The following situations occurred:

case 1:
The first instance was created in scope of another class'  constructor ('Container' in the example).

First I wondered why he was throwing a notice telling me the constant called 'self::ARRAY_KEY' didn't exist, then I figured out that he was searching for it in the wrong class. He tried to use the class 'Container' to initialize the static members of the 'Item' class. Declaring the constant in the 'Container'-class again caused the message to disappear.

case 2:
A dummy instance was created in scope of the class holding the static member.

Now everything worked fine. The class' constructor was called from a static method of the same class.

case 3:
I tried to directly access the static member (array) from outside any class without having created an instance of it before.

In this case I receive a fatal error telling me "Cannot access self:: when no class scope is active"


A workaround would be to omit the self-operator when used in static-initialisations and use the class' name instead.


another strange thing (might be a different bug - please tell me if it is):
When the static array is filled and returned, the key's order is different from that specified in the source. Entries having constant keys (literally entered in the source code) appear before those receiving their key from a class-constant. Each 'group' is ordered as expected for itself.


let me know, if the needed information is complete.

thanks for processing

Reproduce code:
---------------
http://kaiundina.de/bugs/selfbug.php5.txt

(The switch block at the end of the code shows the different use cases and their respective output)

Expected result:
----------------
The static members should be initialized using the constants as declared in the own class rather than using constants declared in the scope of the script that creates the first instance of the class.

In other words:
The 'self' operator should always refer to the class in which it appears when used for initialisation of static members.

Actual result:
--------------
The 'self'-operator, when used for initialisation of static members, refers to the class (if present) that executes the first 'new'-statement for the class containing the static member.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-28 21:00 UTC] sniper@php.net
Please try using this CVS snapshot:

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


 [2005-03-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-08-04 19:47 UTC] bwishan at gmail dot com
I'm experiencing the same problem on PHP 5.0.4.  Has there been any update to this?
 [2006-02-28 23:23 UTC] php at kaiundina dot de
The Problem seems to be fixed in 5.1.2

The arrays are now ordered correctly (case 1 and 2) and the error in case 3 doesn't occur any more.
 [2006-02-28 23:24 UTC] php at kaiundina dot de
forgot to close
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 19:02:15 2014 UTC