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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:42:19+00:00 2026-05-16T12:42:19+00:00

A php closure or anonymous function is used to create function without specifying its

  • 0

A php closure or anonymous function is used to create function without specifying its name.

Is it possible to call them without assigning to identifier as we do in JavaScript ?
e.g.

(function(){
    echo('anonymous function');
})();

What is the correct use of use construct while defining anonymous function and what is the status of anonymous function in public method with accessibility to private properties?

$anon_func = 
function($my_param) use($this->object_property){ //use of $this is erroneous here
    echo('anonymous function');
};
  • 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-05-16T12:42:20+00:00Added an answer on May 16, 2026 at 12:42 pm

    Is it possible to call them without
    assigning to identifier as we do in
    JavaScript ? e.g.

    Not in PHP 5.x; unless you count it when your method takes a callback as an argument. eg:

    $square = array_map(function ($v) { return $v*$v; }, $array);
    

    What is the correct use of use
    construct while defining anonymous
    function

    The use keyword indicates which variables from the current lexical scope should be imported into the closure. You can even pass them by reference and change the variable being passed, eg:

    $total = 0;
    array_walk($array, function ($v) use (&$total) { $total += $v; });
    // $total is now the sum of elements in $array
    

    what is the status of anonymous
    function in public method with
    accessibility to private properties?

    Closures defined inside a class have full access to all its properties and methods, including private ones with no need to import $this through the keyword use in PHP 5.4:

    // this works fine in PHP 5.4
    $anon_func = 
    function($my_param) { 
        $thing = $my_param + $this->object_property;
        echo('anonymous function');
    };
    

    Note that for some strange reason support for $this in closures was removed in PHP 5.3. In this version, you can work around this restriction using something like:

    // a workaround for PHP 5.3
    $temp = $this;
    
    $anon_func = 
    function($my_param) use ($temp) { 
        $thing = $my_param + $temp->object_property;
        echo('anonymous function');
    };
    

    But this gives you access to public members only, attempting to access private members will still give you an error.

    Also note that attempting to import $this (via use), regardless of the PHP version, will result in a fatal error Cannot use $this as lexical variable.

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

Sidebar

Related Questions

Possible Duplicate: How can I use PHP closure function like function() use() on PHP
How can I use PHP closure function like function() use() on PHP 5.2 version
To quote PHP: Anonymous functions are currently implemented using the Closure class. This is
Are closures possible in PHP 5.3? I'm looking for a simple closure example, minimal
Possible Duplicate: Is it possible to access outer local variable in PHP? PHP closure
Is it possible in PHP to require an arbitrary file without leaking any variables
I want to create and execute a closure within a string in PHP and
When I run php-closure i get a PHP error Undefined index: HTTP_IF_NONE_MATCH in <b>/php-closure.php</b>
PHP script to read in user action requests and parse them to their components.
PHP: I have made up a function that returns an array. I would like

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.