xref: /PHP-7.0/Zend/tests/closure_044.phpt (revision e9a95d78)
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
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
54bool(false)
55bool(false)
56
57After binding, null scope, with instance
58bool(false)
59bool(true)
60
61bool(false)
62bool(true)
63
64After binding, with scope, no instance
65bool(true)
66bool(false)
67
68bool(true)
69bool(false)
70
71After binding, with scope, with instance
72bool(true)
73bool(true)
74
75bool(true)
76bool(true)
77
78Done.
79