php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27219 Unable to create static instance if no member vars are declared
Submitted: 2004-02-11 08:49 UTC Modified: 2005-06-22 21:30 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:0 of 0 (0.0%)
From: ivo at ibuildings dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS-2005-01-25 (4 only!) OS: *
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: ivo at ibuildings dot nl
New email:
PHP Version: OS:

 

 [2004-02-11 08:49 UTC] ivo at ibuildings dot nl
Description:
------------
When a class does not have member variables, creating a static instance of the class becomes impossible. See the code below. If the member variable is declared, one onstance is created, and each next call to getInstance will return the existing instance.Without the member var, new instances are created every time.

Reproduce code:
---------------
class mySingleton
{
  // var $m_dummy = ""; // uncommenting this line is a 
                        // workaround

  function &getInstance()
  {
    static $s_instance = NULL;
    if ($s_instance == NULL)
    {
      echo "Creating a new instance<br>";
      $s_instance = new mySingleton();
    }
    else
    {
      echo "Using existing instance<br>";
    }
    return $s_instance;
  }
}

$tmp = &mySingleton::getIntance();
$tmp2 = &mySingleton::getIntance();



Expected result:
----------------
Creating a new instance
Using existing instance

Actual result:
--------------
Creating a new instance
Creating a new instance

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-11 12:31 UTC] sniper@php.net
Works fine with latest PHP5 CVS checkout. Only borks in PHP 4.

 [2005-06-22 21:30 UTC] tony2001@php.net
Because in PHP4 empty object is == NULL, so you have to use === operator, as you're doing it with empty strings, 0 etc.
No PHP bug here, it's well documented.
See here: http://www.php.net/manual/en/migration5.incompatible.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 22 01:01:31 2024 UTC