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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:08:25+00:00 2026-05-27T09:08:25+00:00

The PHP manual states It is not possible to use $this from anonymous function

  • 0

The PHP manual states

It is not possible to use $this from anonymous function before PHP
5.4.0

on the anonymous functions page. But I have found I can make it work by assigning $this to a variable and passing the variable to a use statement at the function definition.

$CI = $this;
$callback = function () use ($CI) {
    $CI->public_method();
};

Is this a good practice?
Is there a better way to access $this inside an anonymous function using PHP 5.3?

  • 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-27T09:08:26+00:00Added an answer on May 27, 2026 at 9:08 am

    It will fail when you try to call a protected or private method on it, because using it that way counts as calling from the outside. There is no way to work around this in 5.3 as far as I know, but come PHP 5.4, it will work as expected, out of the box:

    class Hello {
    
        private $message = "Hello world\n";
    
        public function createClosure() {
            return function() {
                echo $this->message;
            };
        }
    
    }
    $hello = new Hello();
    $helloPrinter = $hello->createClosure();
    $helloPrinter(); // outputs "Hello world"
    

    Even more, you will be able to change what $this points to at runtime, for anonymus functions (closure rebinding):

    class Hello {
    
        private $message = "Hello world\n";
    
        public function createClosure() {
            return function() {
                echo $this->message;
            };
        }
    
    }
    
    class Bye {
    
        private $message = "Bye world\n";
    
    }
    
    $hello = new Hello();
    $helloPrinter = $hello->createClosure();
    
    $bye = new Bye();
    $byePrinter = $helloPrinter->bindTo($bye, $bye);
    $byePrinter(); // outputs "Bye world"
    

    Effectively, anonymus functions will have a bindTo() method, where the first parameter can be used to specify what $this points to, and the second parameter controls what should the visibility level be. If you omit the second parameter, the visibility will be like calling from the “outside”, eg. only public properties can be accessed. Also make note of the way bindTo works, it does not modify the original function, it returns a new one.

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

Sidebar

Related Questions

The most recent comment on PHP's in_array() help page ( http://uk.php.net/manual/en/function.in-array.php#106319 ) states that
The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
The PHP manual for anonymous functions (ie, Closures) states that: Anonymous functions are currently
I have the following piece of code taken from the PHP manual on the
I need to get the basics of this function. The php.net documentation states, for
PHP manual suggests to autoload classes like function __autoload($class_name){ require_once(some_dir/.$class_name..php); } and this approach
PHP Doc (http://php.net/manual/en/language.operators.bitwise.php) clearly states: ~ $a Not Bits that are set in $a
From the PHP manual , it states that: PDO::ATTR_EMULATE_PREPARES Enables or disables emulation of
I think I may have overlooked something here, but I'm not sure why this
In the PHP manual, to show the syntax for functions with optional parameters, they

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.