php.net |  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: kelunik@php.net 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] kelunik@php.net
Description:
------------
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:
---------------
<?php

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

        $foo = true;
    }
}

namespace Test {
    assert();

    var_dump(isset($foo));
}

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-27 09:57 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-10-27 09:57 UTC] nikic@php.net
By design, for the reason you already mentioned. It's also defined in the RFC, see https://wiki.php.net/rfc/expectations#namespaced_assert.
 [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 http://php.net/manual/en/function.assert.php

You cannot expect developers to read old RFC documents to find this out.
 [2017-12-20 14:10 UTC] nikic@php.net
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] kelunik@php.net
Correct, it has been added to https://wiki.php.net/rfc/deprecations_php_7_3 and it will hopefully be accepted to be deprecated in PHP 7.3 and forbidden in PHP 8.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC