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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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: Thu Oct 08 03:00:02 2026 UTC