|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75445 User-defined assert function is optimized away with zend.assertions=-1
Submitted: 2017-10-27 08:48 UTC Modified: 2017-12-20 14:10 UTC
From: Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.2.0RC5 OS: Irrelevant
Private report: No CVE-ID: None
 [2017-10-27 08:48 UTC]
Execute the test script:

 - php -d zend.assertions=1 test.php
 - php -d zend.assertions=0 test.php
 - php -d zend.assertions=-1 test.php

`assert()` doesn't respect the namespace fallback, but assumes unqualified calls are equivalent to `\assert()` and optimize it away / jump around it with assertions disabled.

This is unfortunate, as few people use `\assert()` over `assert()` and wouldn't gain zero-cost assertions without this behavior.

Test script:

namespace Test {
    function assert() {
        global $foo;

        $foo = true;

namespace Test {


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-27 09:57 UTC]
-Status: Open +Status: Not a bug
 [2017-10-27 09:57 UTC]
By design, for the reason you already mentioned. It's also defined in the RFC, see
 [2017-12-20 13:17 UTC] mikko dot rantalainen at peda dot net
The fact that namespaces are honored for assert() BUT the same optimization still happens (the call to assert() is blindly removed) should be explained on page

You cannot expect developers to read old RFC documents to find this out.
 [2017-12-20 14:10 UTC]
IIRC when we discussed this with @kelunik last time, throwing a fatal error if you try to define an assert() function seemed like the best way to resolve this. assert() is essentially a language-construct now, we should treat it as such.
 [2017-12-20 15:05 UTC]
Correct, it has been added to and it will hopefully be accepted to be deprecated in PHP 7.3 and forbidden in PHP 8.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 29 04:01:24 2023 UTC