File: closures_bound_getDeclaringFunction.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 (43 lines) | stat: -rw-r--r-- 1,027 bytes parent folder | download
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
--TEST--
Closures via getParameters() and getDeclaringFunction() calling reflection_method_factory() with an internal object being set
--FILE--
<?php

class A {}
class B extends A {}

$closure = function($op1, $op2 = 0): self { };

$b = new B();
$fn = $closure->bindTo($b, B::class);

const CLOSURE_NAME = '{closure:' . __FILE__ . ':6}';

$rClosure = new ReflectionFunction($fn);
var_dump($rClosure->name == CLOSURE_NAME);

$params = $rClosure->getParameters();
unset ($rClosure);
$closureFromParam = $params[0]->getDeclaringFunction();
var_dump($closureFromParam::class);
var_dump($closureFromParam->name == CLOSURE_NAME);

$rClass = new ReflectionClass($b);
$rMethods = $rClass->getMethods();
var_dump($rMethods);

try {
    $rMethod = new ReflectionMethod($b, CLOSURE_NAME);
    var_dump($rMethod);
} catch (\Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECTF--
bool(true)
string(16) "ReflectionMethod"
bool(true)
array(0) {
}
ReflectionException: Method B::{closure:%s:6}() does not exist