phpdeprecatedassertionphp-8.3

How to fix the now deprecated assert_options?


Consider this code:

        // Make assertions throw exception
        assert_options(
            ASSERT_CALLBACK,
            function ($file, $line, $code, $desc = null) {
                $dest = $file . ':' . $line . ' - ' . $code;
                throw new \Exception($dest . PHP_EOL . $desc);
            }
        );
        assert_options(ASSERT_ACTIVE, 1);
        assert_options(ASSERT_WARNING, 1);

As of PHP 8.3., assert_options is deprecated and we should use some alternative. The documentation provides a hint on this:

Note: The use of assert_options() is discouraged in favor of setting and getting the php.ini directives zend.assertions and assert.exception with ini_set() and ini_get(), respectively.

But how can I do that? What settings should be set via ini_set? For example here assert_options is setting ASSERT_CALLBACK and a callback to be called on failed assertions. How should I do this with ini_set?


Solution

  • I ended up having

            ini_set('zend.assertions', 1);
            ini_set('assert.exception', 1);