xref: /PHP-7.4/Zend/tests/bug32322.phpt (revision 782352c5)
1--TEST--
2Bug #32322 (Return values by reference broken( using self::),example singleton instance)
3--INI--
4error_reporting=4095
5--FILE--
6<?php
7class test
8{
9    private static $instance = null;
10    private $myname = '';
11
12    private function __construct( $value = '' )
13    {
14        echo "New class $value created \n";
15        $this -> myname = $value;
16    }
17    private function __clone() {}
18    static public function getInstance()
19    {
20        if ( self::$instance == null )
21        {
22            self::$instance = new test('Singleton1');
23        }
24        else {
25            echo "Using old class " . self::$instance -> myname . "\n";
26        }
27        return self::$instance;
28    }
29    static public function getInstance2()
30    {
31        static $instance2 = null;
32        if ( $instance2 == null )
33        {
34            $instance2 = new test('Singleton2');
35        }
36        else {
37            echo "Using old class " . $instance2 -> myname . "\n";
38        }
39        return $instance2;
40    }
41    public function __destruct()
42    {
43        if ( defined('SCRIPT_END') )
44        {
45            echo "Class " . $this -> myname . " destroyed at script end\n";
46        } else {
47            echo "Class " . $this -> myname . " destroyed beforce script end\n";
48        }
49    }
50}
51echo "Try static instance inside class :\n";
52$getCopyofSingleton    = test::getInstance();
53$getCopyofSingleton    = null;
54$getCopyofSingleton    = &test::getInstance();
55$getCopyofSingleton    = null;
56$getCopyofSingleton    = test::getInstance();
57echo "Try static instance inside function :\n";
58$getCopyofSingleton2   = test::getInstance2();
59$getCopyofSingleton2   = null;
60$getCopyofSingleton2   = &test::getInstance2();
61$getCopyofSingleton2   = null;
62$getCopyofSingleton2   = test::getInstance2();
63
64define('SCRIPT_END',1);
65?>
66--EXPECTF--
67Try static instance inside class :
68New class Singleton1 created
69Using old class Singleton1
70
71Notice: Only variables should be assigned by reference in %sbug32322.php on line 49
72Using old class Singleton1
73Try static instance inside function :
74New class Singleton2 created
75Using old class Singleton2
76
77Notice: Only variables should be assigned by reference in %sbug32322.php on line 55
78Using old class Singleton2
79Class Singleton1 destroyed at script end
80Class Singleton2 destroyed at script end
81