From:             ninzya at inbox dot lv
Operating system: any
PHP version:      5.3SVN-2009-10-20 (SVN)
PHP Bug Type:     Feature/Change Request
Bug description:  Add possibility to pass callbacks to assert()

Description:
------------
The current assert() implementation is very uncomfortable, for me at
least. assert() does not skip code execution inside it's parentheses when
assertions are disabled. Also, when you are using strings to optimize
runtime of app having full of assert()s around in cases like this - assert(
'return false;') - is very hard to read, and, if variables are being also
involved, the code looks very ugly and is error prone.

I would like to propose to take the full advantage of closures and add
possibility to pass callbacks to assert() function. Passing callbacks to
this function would solve the both problems - bring in the readability, and
would optimize runtime by not calling callbacks when assertions are
disabled. The effect would be the same as if using strings as parameters,
but the code would become more readable a look more nicer.

Here is an example of how does assert() code look like in current PHP
versions. Assume foo() is a method of an object:

function foo( $bar) {
  // This way of testing assertion
  //  results in a low performance, because when you disable
  //  assertions, the code between parentheses is still being
  //  executed, thus, resulting in a waste of computer resources.
  assert( !$bar->testAssert( '5'));

  // Another way of testing assertion. This is more optimized,
  //  because the passed string is not eval()ed when assertion
  //  checking is disabled. Good, but... strings? Unreadable,
  //  ugly, error prone. Yes, i know, i could use double-quotes
  //  here, but that's not the point, strings are no way to good,
  //  maintainable code.
  assert( '!$bar->testAssert( \'5\')');
}

Here is an example of what i would like to see in PHP:

function foo( $bar) {
  // The new way of testing assertion. Code is good, IDEs can take
  //  advantage of a PHP syntax and highlight the statements for me
  //  in my closure. Readable, sexy, way better, than having strings.
  //  Also, closures allow to encapsulate larger blocks of code, which
  //  with string approach would result in a multi-line string
  //  concatenation hell.
  assert( function(){
    return $bar->testAssert( 5);
  });

  // As closures are callbacks by their type, the string callback also
  //  should be valid as assert()'s param:
  assert( 'MyClass::myTestAssertion');
}

And here comes the problem, right, how to distinguish between strings of
code ('!$bar->testAssert( \'5\')') and strings - callbacks
('MyClass::myTestAssertion')? Right, there's no good way to do this, so i
propose to create a separate version of assert(), for example, assert_cb()
(assert by callback), to maintain compatibility with those, who use the old
assertion testing approach.

Reproduce code:
---------------
-

Expected result:
----------------
-

Actual result:
--------------
-

-- 
Edit bug report at http://bugs.php.net/?id=49934&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49934&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49934&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49934&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49934&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49934&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49934&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49934&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49934&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49934&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49934&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49934&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49934&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49934&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49934&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49934&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49934&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49934&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49934&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49934&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49934&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49934&r=mysqlcfg

Reply via email to