Consider this simple child class of PrettyPageHandler
class NewErrorHandler extends \Whoops\Handler\PrettyPageHandler
{
public $foo = null;
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->foo = 'bar';
echo 'executed';
}
}
When this is used like this:
$errorHandler = new NewErrorHandler();
$whoops->pushHandler($errorHandler);
$whoops->register();
var_dump($errorHandler->foo);
$errorHandler->foo is null but "executed" was printed. It seems $this->foo = 'bar'; was not executed or nullified in the process even though it should've. Any ideas why?
Update 1
I'm trying to get the output of handle(); via output buffering which works fine. But at the same time I want to set a var:
public function handle()
{
parent::handle();
$output = ob_get_clean();
echo 'executed';
$this->foo = 'bar';
}
$this->foo = 'bar'; doesn't seem to get executed at this point but "executed" is printed.
Update 2
I'm trying to determine if the error is not yet logged, if true, do stuff like send notifications (e.g., email/sms/etc). If the error is already logged, it means it's duplicate, don't send notifications anymore. Here's a bit more code but simplified for clarity:
<?php
class NewErrorHandler extends \Whoops\Handler\PrettyPageHandler
{
private $errorHash;
private $isNewError;
public function __construct()
{
parent::__construct();
}
public function handle()
{
parent::handle();
$output = ob_get_clean();
$this->setErrorHash();
if (!$this->isDuplicateError()) {
$this->isNewError = true;
$this->log($output);
}
}
private function log(string $data): void
{
$filename = $this->errorHash . '.html';
file_put_contents('logs/errors/'. $filename, $data);
}
public function isDuplicateError(): bool
{
// code check if the file $this->errorHash.'.html' exists in logs/errors/
// ...
}
private function setErrorHash(): string
{
$ex = $this->getException();
$this->errorHash = md5($ex->getMessage() . $ex->getFile() . $ex->getLine() . $ex->getTraceAsString());
}
}
$errorHandler = new NewErrorHandler();
$whoops->pushHandler($errorHandler);
$whoops->register();
if ($errorHandler->isNewError == true)
{
// do something like send notifications... outside the child class.
}
The issue is $errorHandler->isNewError is null even if it should be true
Update 3:
I'll try to explain my thinking on why this should work. Here's a basic example of what I'm trying to do. Let's pretend there's a simplified Whoops class called FakeWhoops
<?php
class FakeWhoops
{
private $handler;
public function pushHandler(CustomHandler $handler)
{
$this->handler = $handler;
}
public function triggerError()
{
$this->handler->handle();
}
}
And this is my custom handler:
class CustomHandler
{
public $foo;
public function handle()
{
$this->foo = true;
echo 'executed';
}
}
Now let's run it and trigger a fake error:
$cH = new CustomHandler;
$fW = new FakeWhoops;
$fW->pushHandler($cH);
// let's trigger a fake error which should call CustomHandler::handle();
$fW->triggerError();
var_dump($cH->foo); // returns true, foo was set!
As you can see here, this is exactly what I'm trying to do albeit using the real Whoops. It's the same simple idea. But why does this work on this simplified example but not with Whoops?