#32322 [Fbk->Opn]: Return values by reference broken( using self::),example singleton instance
| From: | rickd at commando-pernod dot net | Date: | Sun, 20 Mar 2005 05:07:15 +0000 |
| Subject: | #32322 [Fbk->Opn]: Return values by reference broken( using self::),example singleton instance | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-75551@lists.php.net to get a copy of this message | ||
ID: 32322
User updated by: rickd at commando-pernod dot net
-Summary: OOP singleton instance
Reported By: rickd at commando-pernod dot net
-Status: Feedback
+Status: Open
Bug Type: Zend Engine 2 problem
Operating System: Win2000
PHP Version: 5.0.3
New Comment:
<?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
Previous Comments:
------------------------------------------------------------------------
[2005-03-19 21:49:04] sniper@php.net
Please provide an example script that actually returns something. And
give the expected / actual results too.
------------------------------------------------------------------------
[2005-03-19 07:47:09] rickd at commando-pernod dot net
CVS snapshot the same, getInstance() returning reference none copy of
var, only way at the moment is this little workaround ( when using
private static in class not in function ):
public static function getInstance()
{
if ( self::$instance == null )
{
self::$instance = new test();
}
else {
echo "old";
}
return ( $r =& self::$instance);
}
------------------------------------------------------------------------
[2005-03-18 19:26:19] 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-15 21:03:10] rickd at commando-pernod dot net
same but static inside class function and cant be killed with
referencing, remember in the other code i dont use &getinstance() so
the function do not return a reference how its describe in manual (
both need reference, caller and function code)
class test {
static function getinstance() {
static $instance;
if ( $instance == null ) {
$instance = new test();
}
return $instance;
}
}
$user = &test::getinstance();
$user = null; // dont destroy instance
$user = &test::getinstance();
unset( $user ); // dont destroy instance
------------------------------------------------------------------------
[2005-03-15 21:00:00] rickd at commando-pernod dot net
upps i did i typing mistake :
so i mean
class test {
static private $instance = null;
static function getinstance() {
if ( self::$instance == null ) {
self::$instance = new test();
}
return self::$instance;
}
}
$user = &test::getinstance();
$user = null; // destroy singleton instance
$user = &test::getinstance();
unset( $user ); // dont destroy instance
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/32322
--
Edit this bug report at http://bugs.php.net/?id=32322&edit=1