3v4l.org

run code in 500+ PHP versions simultaneously
<?php class YoProvider { protected function getYo() { return 'yo'; } } trait testTrait { function invokeProtectedMethod($object, $method) { $rm = new ReflectionMethod($object, $method); if ($rm->isProtected()) { $rm->setAccessible(TRUE); return $rm->invoke($object); } } } class testClass { use testTrait; function testYoProvider() { $yo_provider = new YoProvider(); $yo = $this->invokeProtectedMethod($yo_provider, 'getYo'); print $yo; // $this->assertEquals('yo', $yo); } } (new testClass())->testYoProvider();
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24
yo
Output for 8.5.0
Deprecated: Method ReflectionMethod::setAccessible() is deprecated since 8.5, as it has no effect in /in/rK9r7 on line 13 yo
Output for 8.5.1 - 8.5.9
Deprecated: Method ReflectionMethod::setAccessible() is deprecated since 8.5, as it has no effect since PHP 8.1 in /in/rK9r7 on line 13 yo

preferences:
32.59 ms | 1141 KiB | 11 Q