xref: /PHP-7.3/Zend/tests/closure_043.phpt (revision b746e698)
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);
30$d = $staticScoped->bindTo(new A, null);
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');
38$d = $staticScoped->bindTo(new A, 'A');
39
40echo "Done.\n";
41--EXPECTF--
42Before binding
43bool(false)
44bool(false)
45
46bool(true)
47bool(false)
48
49After binding, null scope, no instance
50bool(false)
51bool(false)
52
53bool(false)
54bool(false)
55
56After binding, null scope, with instance
57
58Warning: Cannot bind an instance to a static closure in %s on line %d
59
60Warning: Cannot bind an instance to a static closure in %s on line %d
61After binding, with scope, no instance
62bool(true)
63bool(false)
64
65bool(true)
66bool(false)
67
68After binding, with scope, with instance
69
70Warning: Cannot bind an instance to a static closure in %s on line %d
71
72Warning: Cannot bind an instance to a static closure in %s on line %d
73Done.
74