xref: /PHP-5.5/Zend/tests/closure_043.phpt (revision 38ff70ef)
1--TEST--
2Closure 043: Scope/bounding combination invariants; static closures
3--FILE--
4<?php
5/* Whether it's scoped or not, a static closure cannot have
6 * a bound instance. It should also not be automatically converted
7 * to a non-static instance when attempting to bind one */
8
9$staticUnscoped = static function () { var_dump(isset(A::$priv)); var_dump(isset($this)); };
10
11class A {
12	private static $priv = 7;
13	static function getStaticClosure() {
14		return static function() { var_dump(isset(A::$priv)); var_dump(isset($this)); };
15	}
16}
17
18$staticScoped = A::getStaticClosure();
19
20echo "Before binding", "\n";
21$staticUnscoped(); echo "\n";
22$staticScoped(); echo "\n";
23
24echo "After binding, null scope, no instance", "\n";
25$d = $staticUnscoped->bindTo(null, null); $d(); echo "\n";
26$d = $staticScoped->bindTo(null, null); $d(); echo "\n";
27
28echo "After binding, null scope, with instance", "\n";
29$d = $staticUnscoped->bindTo(new A, null); $d(); echo "\n";
30$d = $staticScoped->bindTo(new A, null); $d(); echo "\n";
31
32echo "After binding, with scope, no instance", "\n";
33$d = $staticUnscoped->bindTo(null, 'A'); $d(); echo "\n";
34$d = $staticScoped->bindTo(null, 'A'); $d(); echo "\n";
35
36echo "After binding, with scope, with instance", "\n";
37$d = $staticUnscoped->bindTo(new A, 'A'); $d(); echo "\n";
38$d = $staticScoped->bindTo(new A, 'A'); $d(); echo "\n";
39
40echo "Done.\n";
41
42--EXPECTF--
43Before binding
44bool(false)
45bool(false)
46
47bool(true)
48bool(false)
49
50After binding, null scope, no instance
51bool(false)
52bool(false)
53
54bool(false)
55bool(false)
56
57After binding, null scope, with instance
58
59Warning: Cannot bind an instance to a static closure in %s on line %d
60bool(false)
61bool(false)
62
63
64Warning: Cannot bind an instance to a static closure in %s on line %d
65bool(false)
66bool(false)
67
68After binding, with scope, no instance
69bool(true)
70bool(false)
71
72bool(true)
73bool(false)
74
75After binding, with scope, with instance
76
77Warning: Cannot bind an instance to a static closure in %s on line %d
78bool(true)
79bool(false)
80
81
82Warning: Cannot bind an instance to a static closure in %s on line %d
83bool(true)
84bool(false)
85
86Done.
87