php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69269 Allow assert to execute a closure
Submitted: 2015-03-20 12:18 UTC Modified: 2015-03-20 12:59 UTC
From: tom at r dot je Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.6.6 OS: any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tom at r dot je
New email:
PHP Version: OS:

 

 [2015-03-20 12:18 UTC] tom at r dot je
Description:
------------
Currently the manual helpfully states on http://php.net/manual/en/function.assert.php that " The advantages of a string assertion are less overhead when assertion checking is off"

Which makes perfect sense. The disadvantages, of course, are that readability is reduced and IDE hints/highlighting won't work and the parser won't immediately catch parse errors in the string.

As a happy medium I propose it would be better if assert() could take a closure as it's first argument:

assert(function() {
	return 2 < 1;
});

This has the best of both worlds as the code wouldn't be executed unless assertions are turned on.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-20 12:59 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2015-03-20 12:59 UTC] nikic@php.net
PHP 7 will provide an ini option to disable assert without any runtime cost, see https://wiki.php.net/rfc/expectations. Which is what you want, only without having to use a closure.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 23:00:01 2026 UTC