rfc:true-nested-function-support

Request for Comments: True nested function support

Introduction

Stub

Why nested functions are useless at the moment

  • They exist in the global namespace.
  • If the parent function is called twice they will be defined twice and thus give a fatal error.

Code examples

1) Current idiomatic usage of helper functions

    function _prefix_functionname_helperfunctionname($arg1, $arg2) {
        ;
    }
 
    function prefix_functionname() {
       // Code here...
       somefunc(prefix_functionname_helperfunctionname($arg1, $arg2));
    }

2) Appending the function name and wrapping in function_exists()

    function prefix_functionname() {
       if (!function_exists('prefix_functionname_innerfunctionname')) {
           function prefix_functionname_innerfunctionname($arg1, $arg2) {
               ;
           }
       }
       // Code here...
       somefunc(prefix_functionname_innerfunctionname($arg1, $arg2));
    }

3) Using closures

    function prefix_functionname() {
       $innerfunctionname = function ($arg1, $arg2) {
           ;
       }
       // Code here...
       somefunc($innerfunctionname($arg1, $arg2));
    }

4) Using new syntax

    function prefix_functionname() {
       function innerfunctionname($arg1, $arg2) {
           ;
       }
       // Code here...
       somefunc(innerfunctionname($arg1, $arg2));
    }

Todo for this RFC

  • Write little script that checks for the current use of nested functions in common frameworks like Symphony and Zend. I am assuming this is zero.
rfc/true-nested-function-support.txt · Last modified: 2017/09/22 13:28 (external edit)