3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Fake\BuiltIn\Functions; /** * Executes the given statements using fake built-in functions. * * @param callable $statements Statements to execute. * @return mixed Whatever $statements returns. * @throws \ReflectionException */ function fake(callable $statements) { $function = new \ReflectionFunction($statements); $start_line = $function->getStartLine(); $end_line = $function->getEndLine(); $function_source = implode('', array_slice(file($function->getFileName()), $start_line - 1, $end_line - $start_line + 1)); if (preg_match('/(?<={).*(?=})/s', $function_source, $matches)) { $function_body = $matches[0]; $namespace = __NAMESPACE__; return eval(" namespace $namespace; $function_body "); } throw new \RuntimeException('Failed to execute statements.'); } function strlen($string) { return 'fake result'; } namespace My\Name\Space; use function Fake\BuiltIn\Functions\fake; class MyClass { /** * @throws \ReflectionException */ public function testMethodWithFakeStrlen() { fake(function () { echo strlen('foo'), PHP_EOL; }); } public function testMethodWithoutFakeStrlen() { echo strlen('foo'), PHP_EOL; } } $myObject = new MyClass(); try { $myObject->testMethodWithFakeStrlen(); $myObject->testMethodWithoutFakeStrlen(); } catch (\ReflectionException $e) { // ... }
Output for git.master, git.master_jit, rfc.property-hooks
fake result 3

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
28.96 ms | 401 KiB | 8 Q