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 9080343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:03:32+00:00 2026-06-16T20:03:32+00:00

When using anonymous functions in PHP, you can easily use variables from right outside

  • 0

When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the use() keyword.

In my case the anonymous functions are already defined somewhere, but called later on (somewhere else) in a class.

The following piece of code is to illustrate the idea:

<?php

$bla = function ( $var1 ) use ($arg)
        {
            echo $var1;
        };

class MyClass
{
    private $func;

    public function __construct ( $func )
    {
        $this->func = $func;
    }

    public function test ( $arg )
    {
        $closure =  $this->func;
        $closure ( 'anon func' );
    }
}

$c = new MyClass($bla);
$c->test ( 'anon func' );

What i’m doing is i create an anonymous function and store that in a variable. I pass that variable to the method of a class and that is where i want to run the anonymous function.

But i can’t use the use() keyword to get the $arg parameter from the method this way. Because the anonymous function was declared outside of the method.

But i really need a way to get the variables from the method where the anonymous function is run from. Is there a way to do that, when the anonymous function is declared somewhere else..?

  • 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-16T20:03:33+00:00Added an answer on June 16, 2026 at 8:03 pm

    The point of the use keyword is to inherit/close over a particular environment state from the parent scope into the Closure when it’s defined, e.g.

    $foo = 1;
    
    $fn = function() use ($foo) {
        return $foo;
    };
    
    $foo = 2;
    
    echo $fn(); // gives 1
    

    If you want $foo to be closed over at a later point, either define the closure later or, if you want $foo to be always the current value (2), pass $foo as a regular parameter.

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

Sidebar

Related Questions

To quote PHP: Anonymous functions are currently implemented using the Closure class. This is
The PHP manual states It is not possible to use $this from anonymous function
Can OOP programming be emulated with just normal functions? Like with using static variables
Using the C# object initializer syntax I can instantiate an anonymous object like this:
How can I use PHP 5.3 Closures like We use Blocks in Ruby. I
Possible Duplicate: What are the benefits to using anonymous functions instead of named functions
I know that with anonymous functions, local stack variables are promoted to a class,
I using PHP/Javascript - Widget of alexmarandon from link http://alexmarandon.com/articles/web_widget_jquery/ And in project of
I have a need for anonymous functions in php (soundcloud php wrapper) and so
As the official documentation say I'm using an anonymous PHP function in my ZF2

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.