Zend Framework

don't use "create_function" if possible

Details

  • Type: Sub-task Sub-task
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Not an Issue
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Activity

Hide
Marc Bennewitz (GIATA mbH) added a comment -

Use a real function instead because:

  • the function byte code can't cache by apc and others
  • the return value is only a link the the function after removing this you can't call the function but it is in memory.
    (This seduce developers to to create a function on each function call like preg_match_callback)
    Memory-Test
    var_dump(memory_get_usage(true));
    for ($i=0; $i<1000; $i++) {
        create_function('$arg', '$test = "test";');
    }
    var_dump(memory_get_usage(true));
    for ($i=0; $i<1000; $i++) {
        // same as above but allocates 3x more memory.
        create_function('$arg', '$test = "test";'
                              . '$test = "test";'
                              . '$test = "test";');
    }
    var_dump(memory_get_usage(true));
Used locations

#grep -iHrn 'create_function' * | grep -v 'svn'
libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:104: create_function(
libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:116: create_function(
libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:128: create_function(
libraries/framework.zend.com/Zend/Feed/Element.php:170: return array_map(create_function('$e', 'return new Zend_Feed_Element($e);'), $nodes);

Show
Marc Bennewitz (GIATA mbH) added a comment - Use a real function instead because:
  • the function byte code can't cache by apc and others
  • the return value is only a link the the function after removing this you can't call the function but it is in memory. (This seduce developers to to create a function on each function call like preg_match_callback)
    Memory-Test
    var_dump(memory_get_usage(true));
    for ($i=0; $i<1000; $i++) {
        create_function('$arg', '$test = "test";');
    }
    var_dump(memory_get_usage(true));
    for ($i=0; $i<1000; $i++) {
        // same as above but allocates 3x more memory.
        create_function('$arg', '$test = "test";'
                              . '$test = "test";'
                              . '$test = "test";');
    }
    var_dump(memory_get_usage(true));
Used locations
#grep -iHrn 'create_function' * | grep -v 'svn' libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:104: create_function( libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:116: create_function( libraries/framework.zend.com/Zend/Dom/Query/Css2Xpath.php:128: create_function( libraries/framework.zend.com/Zend/Feed/Element.php:170: return array_map(create_function('$e', 'return new Zend_Feed_Element($e);'), $nodes);
Hide
Matthew Weier O'Phinney added a comment -

The places where create_function() are used are (a) not used for many situations, and (b) appropriate use cases. When we move to PHP 5.3 as the minimum supported version, these will be replaced with closures, but until then, there is no reason to change.

Show
Matthew Weier O'Phinney added a comment - The places where create_function() are used are (a) not used for many situations, and (b) appropriate use cases. When we move to PHP 5.3 as the minimum supported version, these will be replaced with closures, but until then, there is no reason to change.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: