File: closure_compare.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (114 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
--TEST--
Closure comparison
--FILE--
<?php
function foo() {
    static $var;
}

$closures[0] = Closure::fromCallable('foo');
$closures[1] = Closure::fromCallable('foo');

printf("foo == foo: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable('strlen');
$closures[1] = Closure::fromCallable('strlen');

printf("strlen == strlen: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable('strlen');
$closures[1] = Closure::fromCallable('strrev');

printf("strlen != strrev: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

trait MethodTrait {
    public function traitMethod(){}
}

class Foo {
    use MethodTrait {
        MethodTrait::traitMethod as aliasMethod;
    }

    public function __call($method, $args) {
    
    }

    public function exists() {}

    public static function existsStatic() {}
}

class Bar extends Foo {}

class Baz {
    use MethodTrait;
}

$closures[0] = Closure::fromCallable([Foo::class, "existsStatic"]);
$closures[1] = Closure::fromCallable([Bar::class, "existsStatic"]);

printf("foo::existsStatic != bar::existsStatic: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$foo = new Foo;

$closures[0] = Closure::fromCallable([$foo, "exists"]);
$closures[1] = $closures[0]->bindTo(new Foo);

printf("foo#0::exists != foo#1::exists: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$baz = new Baz;

$closures[0] = Closure::fromCallable([$foo, "traitMethod"]);
$closures[1] = Closure::fromCallable([$baz, "traitMethod"]);

printf("foo::traitMethod != baz::traitMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable([$foo, "traitMethod"]);
$closures[1] = Closure::fromCallable([$foo, "aliasMethod"]);

printf("foo::traitMethod != foo::aliasMethod: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable([$foo, "exists"]);
$closures[1] = Closure::fromCallable([$foo, "exists"]);

printf("foo::exists == foo::exists: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable([$foo, "method"]);
$closures[1] = Closure::fromCallable([$foo, "method"]);

printf("foo::method == foo::method: %s\n", $closures[0] == $closures[1] ? "OK" : "FAIL");

$closures[1] = $closures[1]->bindTo(new Bar);

printf("foo::method != bar::method: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$closures[0] = Closure::fromCallable([$foo, "method"]);
$closures[1] = Closure::fromCallable([$foo, "method2"]);

printf("foo::method != foo::method2: %s\n", $closures[0] != $closures[1] ? "OK" : "FAIL");

$closures[2] = Closure::fromCallable([$closures[0], "__invoke"]);
$closures[3] = Closure::fromCallable([$closures[1], "__invoke"]);

printf("Closure[0]::invoke != Closure[1]::invoke: %s\n", $closures[2] != $closures[3] ? "OK" : "FAIL");

$closures[2] = Closure::fromCallable([$closures[0], "__invoke"]);
$closures[3] = Closure::fromCallable([$closures[0], "__invoke"]);

printf("Closure[0]::invoke == Closure[0]::invoke: %s\n", $closures[2] == $closures[3] ? "OK" : "FAIL");
?>
--EXPECT--
foo == foo: OK
strlen == strlen: OK
strlen != strrev: OK
foo::existsStatic != bar::existsStatic: OK
foo#0::exists != foo#1::exists: OK
foo::traitMethod != baz::traitMethod: OK
foo::traitMethod != foo::aliasMethod: OK
foo::exists == foo::exists: OK
foo::method == foo::method: OK
foo::method != bar::method: OK
foo::method != foo::method2: OK
Closure[0]::invoke != Closure[1]::invoke: OK
Closure[0]::invoke == Closure[0]::invoke: OK