What are alternatives for assert_options & ASSERT_CALLBACK

99 views Asked by At

I have been using:

assert_options (ASSERT_CALLBACK, function(string $file, int $line, ?string $assertion, string $description = null){
    // my codes
}

However, assert_options has been deprecated as of PHP 8.3. What are other compatible alternatives to hook a function as default assert callback?

1

There are 1 answers

0
T.Todua On

Bard has surfaced a good alternative:

namespace my_ns;

function my_assert_handler(Throwable $e) {
  if ($e instanceof AssertionError) {
    // Your callback logic here
    echo "Assertion failed: " . $e->getMessage() . PHP_EOL;
  }
}

set_exception_handler('my_ns\my_assert_handler');