10

When I try to do this:

function a()
{
    function b() { }
}

a();
a();

I get Cannot redeclare b....
When I tried to do this:

function a()
{
    if(!isset(b))
        function b() { }
}

a();
a();

I get unexpected ), expected ....
How can I declare the function as local and to be forgotten of when a returns? I need the function to pass it to array_filter.

4
  • 3
    nesting functions is asking for a world of hurt. Commented Sep 19, 2011 at 0:43
  • 1
    What version of PHP are you using? Commented Sep 19, 2011 at 0:45
  • @Mark Elliot: The version that produced those errors is PHP Version 5.3.2-1ubuntu4.2, but this has to run on PHP Version 5.3.6 Commented Sep 19, 2011 at 0:47
  • Did you mean "if(!function_exists('b'))" instead of "if(!isset(b))"? if(!function_exists('b')) { function b() { } } should work. Commented Sep 19, 2011 at 8:29

4 Answers 4

9

The idea of "local function" is also named "function inside function" or "nested function"... The clue was in this page, anonymous function, cited by @ceejayoz and @Nexerus, but, let's explain!

  1. In PHP only exist global scope for function declarations;
  2. The only alternative is to use another kind of function, the anonymous one.

Explaining by the examples:

  1. the function b() in function a(){ function b() {} return b(); } is also global, so the declaration have the same effect as function a(){ return b(); } function b() {}.

  2. To declare something like "a function b() in the scope of a()" the only alternative is to use not b() but a reference $b(). The syntax will be something like function a(){ $b = function() { }; return $b(); }

PS: is not possible in PHP to declare a "static reference", the anonymous function will be never static.

See also:

Sign up to request clarification or add additional context in comments.

Comments

8

You could use an anonymous function in your array_filter call.

Comments

3

Warning for PHP 7.2 or above!

create_function is deprecated in php 7.2 and removed in php 8+.

This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.

Replaced by: https://www.php.net/manual/en/functions.anonymous.php


Original answer:

You could try the create_function function PHP has. http://php.net/create_function

$myfunc = create_function(..., ...);
array_filter(..., $myfunc(...));
unset($myfunc);

This method doesn't require PHP 5.3+

Comments

1

You can define local function within PHP, but you can declare it only once. This can be achieved by defining and comparing static variable.

Check the following example:

<?php
function a()
{
  static $init = true;
  if ($init) {
    function b() { }
    $init = FALSE;
  }
}

a();
a();

Alternatively by checking if function already exists:

<?php
function a()
{
  if (!function_exists('b')) {
     function b() { }
  }
}

a();
a();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.