xref: /PHP-8.1/Zend/tests/closure_043.phpt (revision 7aacc705)
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?>
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
60
61Warning: Cannot bind an instance to a static closure in %s on line %d
62After binding, with scope, no instance
63bool(true)
64bool(false)
65
66bool(true)
67bool(false)
68
69After binding, with scope, with instance
70
71Warning: Cannot bind an instance to a static closure in %s on line %d
72
73Warning: Cannot bind an instance to a static closure in %s on line %d
74Done.
75