Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:26:55+00:00 2026-06-05T04:26:55+00:00

In Python you can have the following: def foo(param1, param2): def bar(): print param1

  • 0

In Python you can have the following:

def foo(param1, param2):
    def bar():
        print param1 + param2
    bar()

I am having some difficulties with this behaviour in PHP though.
I expect this to work in the following way:

function foo($param1, $param2)
{
    function bar()
    {
        echo $param1 + $param2;
    }
    bar();
}

But that fails. So I read some about closures (this is called a closure is it not? It is in Python, that I know). And in the php documentation about anonymous functions (which they said to have been implemented as closures) they tell you to use the use() expression in the following manner:

function foo($param1, $param2)
{
    function bar() use($param1, $param2)
    {
        echo $param1 + $param2;
    }
    bar();
}

But that still fails. So I changed it to the PHP-anonymous function a-like like this:

function foo($param1, $param2)
{
    $bar = function() use($param1, $param2)
    {
        echo $param1 + $param2;
    };
    $bar();
}

That does work, however it just looks really ugly. Am I missing something? Can I improve this in any way? Or will I just have to use the ‘ugly’ way?

(I am not looking for a discussion about if closures are useful)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:26:58+00:00Added an answer on June 5, 2026 at 4:26 am

    I couldn’t find the function bar() use($param1, $param2) syntax on the manual page you linked to, just the “ugly” version that works (which is truly anonymous). I guess you’ll have to use that.

    On your second example, bar is not a closure. To create a closure in PHP you have to use the ugly use, or the Closure class. Every function will create its own local scope, but closures are not automatic.

    PHP seems to have an odd definition for the term “closure”, as you probably noted when you read the manual. They define it as a synonym for “anonymous function”:

    Anonymous functions, also known as closures (…)

    Confusing, right? Later, they explain you need the use keyword if you want to inherit the parent scope:

    Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header.

    The PHP Wiki rfc page on closures gives us some hints on why closures were implemented this way:

    PHP’s notion of scope is quite different than the notion of scope other languages define. Combine this with variable variables ($$var) and it becomes clear that automatically detecting which variables from the outer scope are referenced inside are closure is impossible. Also, since for example global variables are not visible inside functions either by default, automatically making the parent scope available would break with the current language concept PHP follows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code (that I can't easily modify), of the following form: def
I am learning python and have this error . I can figure out where\what
In python, If I have a class foo , I can call foo.__module__ to
I have this following python code, it works fine in python but fails with
Suppose I have the following python code: def outer(): string = def inner(): string
Lets say I have two functions: def foo(): return 'foo' def bar(): yield 'bar'
I have the following code in Python: def point_to_index(point): if point not in points:
I have the following strings: I submit the following values: username 'foo', password 'bar'
I have the following two Python functions: @classmethod def serialize_dict(cls, d): values = []
I have the following function in a Python module called dictbuilder.py: def my_dictbuilder(reader_o, writer):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.