php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #12454 Static references are transient inside methods
Submitted: 2001-07-29 05:14 UTC Modified: 2002-06-09 19:16 UTC
From: nick at macaw dot demon dot co dot uk Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nick at macaw dot demon dot co dot uk
New email:
PHP Version: OS:

 

 [2001-07-29 05:14 UTC] nick at macaw dot demon dot co dot uk
Another unfortunate bug with references appears to be that statics holding references inside methods are actually transient, and a reference will be lost.

For example, calling the following code several times will initialise $db and return every time with a new instance.

function &getInstance()
  {
    static $db;

    if (!isset($db)) {
      $db =& new FS_DB();
    }

    return $db;
  }

whereas the following will give true singeton behaviour and initialise just once, as expected.

function &getInstance()
  {
    static $db;

    if (!isset($db)) {
      $db = new FS_DB();
    }

    return $db;
  }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-01 20:10 UTC] mfischer@php.net
Making this a documentation problem.

Static variables just can't hold references of any kind.

For objects it's avoidable with ZE2 because of the new object model.
 [2002-06-09 19:16 UTC] mfischer@php.net
This bug has been fixed in CVS. You can grab a snapshot of the
CVS version at http://snaps.php.net/. In case this was a documentation 
problem, the fix will show up soon at http://www.php.net/manual/.
In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites.
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 09:01:30 2024 UTC