3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private function customErrorHandler(int $code, string $msg): bool { var_dump(preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg)); return true; } protected function write(): void { set_error_handler([$this, 'customErrorHandler']); try { $stream = fopen('logs://foo.log', 'a'); trigger_error('Test test'); } finally { restore_error_handler(); } } public function test() { $this->write(); } } class Bar { public function customErrorHandler(int $code, string $msg): bool { var_dump(preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg)); return true; } protected function write(): void { set_error_handler([$this, 'customErrorHandler']); try { $stream = fopen('logs://foo.log', 'a'); trigger_error('Test test'); } finally { restore_error_handler(); } } public function test() { $this->write(); } } class StreamHandler { public $context; protected $handle; public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool { $path = str_replace('logs://', '/tmp/', $path); $handle = @fopen($path, $mode); if ($handle === false) { return false; } $this->handle = $handle; return true; } } stream_wrapper_register('logs', StreamHandler::class); touch('/tmp/foo.log'); chmod('/tmp/foo.log', 0444); (new Bar)->test(); (new Foo)->test();
Output for 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
string(40) "Failed to open stream: Permission denied" string(63) "Failed to open stream: "StreamHandler::stream_open" call failed" string(9) "Test test" Fatal error: Uncaught Error: Invalid callback Foo::customErrorHandler, cannot access private method Foo::customErrorHandler() in /in/LsBph:59 Stack trace: #0 /in/LsBph(59): fopen('/tmp/foo.log', 'a') #1 [internal function]: StreamHandler->stream_open('/tmp/foo.log', 'a', 0, NULL) #2 /in/LsBph(14): fopen('logs://foo.log', 'a') #3 /in/LsBph(23): Foo->write() #4 /in/LsBph(75): Foo->test() #5 {main} thrown in /in/LsBph on line 59
Process exited with code 255.
Output for 8.1.0 - 8.1.30
string(40) "Failed to open stream: Permission denied" string(63) "Failed to open stream: "StreamHandler::stream_open" call failed" string(9) "Test test" string(63) "Failed to open stream: "StreamHandler::stream_open" call failed" string(9) "Test test"

preferences:
52.84 ms | 408 KiB | 5 Q