xref: /PHP-8.1/Zend/tests/closure_044.phpt (revision 7aacc705)
1--TEST--
2Closure 044: Scope/bounding combination invariants; non static closures
3--FILE--
4<?php
5/* A non-static closure has a bound instance if it has a scope
6 * and doesn't have an instance if it has no scope */
7
8$nonstaticUnscoped = function () { var_dump(isset(A::$priv)); var_dump(isset($this)); };
9
10class A {
11    private static $priv = 7;
12    function getClosure() {
13        return function() { var_dump(isset(A::$priv)); var_dump(isset($this)); };
14    }
15}
16
17$a = new A();
18$nonstaticScoped = $a->getClosure();
19
20echo "Before binding", "\n";
21$nonstaticUnscoped(); echo "\n";
22$nonstaticScoped(); echo "\n";
23
24echo "After binding, null scope, no instance", "\n";
25$d = $nonstaticUnscoped->bindTo(null, null); $d(); echo "\n";
26$d = $nonstaticScoped->bindTo(null, null); var_dump($d); echo "\n";
27
28echo "After binding, null scope, with instance", "\n";
29$d = $nonstaticUnscoped->bindTo(new A, null); $d(); echo "\n";
30$d = $nonstaticScoped->bindTo(new A, null); $d(); echo "\n";
31
32echo "After binding, with scope, no instance", "\n";
33$d = $nonstaticUnscoped->bindTo(null, 'A'); $d(); echo "\n";
34$d = $nonstaticScoped->bindTo(null, 'A'); var_dump($d); echo "\n";
35
36echo "After binding, with scope, with instance", "\n";
37$d = $nonstaticUnscoped->bindTo(new A, 'A'); $d(); echo "\n";
38$d = $nonstaticScoped->bindTo(new A, 'A'); $d(); echo "\n";
39
40echo "Done.\n";
41?>
42--EXPECTF--
43Before binding
44bool(false)
45bool(false)
46
47bool(true)
48bool(true)
49
50After binding, null scope, no instance
51bool(false)
52bool(false)
53
54
55Warning: Cannot unbind $this of closure using $this in %s on line %d
56NULL
57
58After binding, null scope, with instance
59bool(false)
60bool(true)
61
62bool(false)
63bool(true)
64
65After binding, with scope, no instance
66bool(true)
67bool(false)
68
69
70Warning: Cannot unbind $this of closure using $this in %s on line %d
71NULL
72
73After binding, with scope, with instance
74bool(true)
75bool(true)
76
77bool(true)
78bool(true)
79
80Done.
81