xref: /PHP-7.3/Zend/tests/closure_044.phpt (revision b746e698)
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); $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'); $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--EXPECT--
42Before binding
43bool(false)
44bool(false)
45
46bool(true)
47bool(true)
48
49After binding, null scope, no instance
50bool(false)
51bool(false)
52
53bool(false)
54bool(false)
55
56After binding, null scope, with instance
57bool(false)
58bool(true)
59
60bool(false)
61bool(true)
62
63After binding, with scope, no instance
64bool(true)
65bool(false)
66
67bool(true)
68bool(false)
69
70After binding, with scope, with instance
71bool(true)
72bool(true)
73
74bool(true)
75bool(true)
76
77Done.
78