php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32322 Return values by reference broken( using self::),example singleton instance
Submitted: 2005-03-15 19:54 UTC Modified: 2005-06-08 10:44 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: rickd at commando-pernod dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-03-19 OS: Win2000
Private report: No CVE-ID: None
 [2005-03-15 19:54 UTC] rickd at commando-pernod dot net
Description:
------------
We use a user singleton instance for our cms user authed ids that should not be able to killed from third party addons or worse coders so easily, but accessable from all.

But when someone get the instance with reference it can be killed easy with setting the reference var to null, unset dont work.

If you put the static $instance holder inside the getinstance() function it seems to be work correct and cant be deleted from setting reference to NULL



Reproduce code:
---------------
class test {
   static private $instance = null;
   static function getinstance() {
      if ( self::$instance == null ) {
         return new test();
      }
      return self::$instance;
   }
}
$user = &test::getinstance();
$user = null; // destroy singleton instance
$user = &test::getinstance();
unset( $user ); // dont destroy instance

Expected result:
----------------
singleton not destroying with setting a getted instance with
reference to null

Actual result:
--------------
singleton destroyed

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-19 21:49 UTC] sniper@php.net
Please provide an example script that actually returns something. And give the expected / actual results too.

 [2005-03-20 06:07 UTC] rickd at commando-pernod dot net
<?php
class test
{
    private static $instance = null;
    private $myname = '';
    
    private function __construct( $value = '' ) 
    {
        echo "New class $value created \n";
        $this -> myname = $value;
    }
    private function __clone() {}
    static public function getInstance()
    {
        if ( self::$instance == null )
        {
            self::$instance = new test('Singleton1');
        }
        else {
            echo "Using old class " . self::$instance -> myname . "\n";
        }
        return self::$instance;
    }
    static public function getInstance2()
    {
        static $instance2 = null;
        if ( $instance2 == null )
        {
            $instance2 = new test('Singleton2');
        }
        else {
            echo "Using old class " . $instance2 -> myname . "\n";
        }
        return $instance2;
    }
    public function __destruct() 
    {
        if ( defined('SCRIPT_END') )
        {
            echo "Class " . $this -> myname . " destroyed at script end \n";
        } else {
            echo "Class " . $this -> myname . " destroyed beforce script end \n";
        }
    }
}    
echo "Try static instance inside class :\n";
$getCopyofSingleton    = test::getInstance();
$getCopyofSingleton    = null;
$getCopyofSingleton    = &test::getInstance();
$getCopyofSingleton    = null;
$getCopyofSingleton    = test::getInstance();
echo "Try static instance inside function :\n";
$getCopyofSingleton2   = test::getInstance2();
$getCopyofSingleton2   = null;
$getCopyofSingleton2   = &test::getInstance2();
$getCopyofSingleton2   = null;
$getCopyofSingleton2   = test::getInstance2();

define('SCRIPT_END',1);
?>

Current result :

Try static instance inside class :
New class Singleton1 created 
Using old class Singleton1
Class Singleton1 destroyed beforce script end 
New class Singleton1 created 
Try static instance inside function :
New class Singleton2 created 
Using old class Singleton2
Using old class Singleton2
Class Singleton1 destroyed at script end 
Class Singleton2 destroyed at script end 

Expected result :

Try static instance inside class :
New class Singleton1 created 
Using old class Singleton1
Using old class Singleton1
Try static instance inside function :
New class Singleton2 created 
Using old class Singleton2
Using old class Singleton2
Class Singleton1 destroyed at script end 
Class Singleton2 destroyed at script end 

php setting :
allow_call_time_pass_reference Off Off 
zend.ze1_compatibility_mode Off Off 

What i mean whats going wrong :
to return a variable by reference, you need to define the caller?s code and function code with a & prefix, but it seems when you use the self:: and parent:: as return values
this is broken, only caller need a & prefix, that is what i mean
 [2005-06-08 10:44 UTC] dmitry@php.net
This bug is already fixed in CVS HEAD and PHP_5_0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 16:01:31 2024 UTC