<?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();
preferences:
15.39 ms | 414 KiB | 5 Q