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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:02:08+00:00 2026-05-24T17:02:08+00:00

( I understand this may be a touchy decision, but I’d appreciate we avoid

  • 0

(I understand this may be a touchy decision, but I’d appreciate we avoid answers such as “Please don’t.”)

I have a class whose constructor takes a callback as an argument. In this callback, I think it would be most semantically correct that the $this variable be available as a reference to the instance to which the callback belongs. (I’ve noticed that 5.4 has reinstated $this in the context of anonymous functions defined within a class, however even that change wouldn’t help here as the function is passed as an argument to the constructor as mentioned)

The issue comes from the fact that the instance is not being passed as an argument to the callback, but instead made available by use()

use() has the tendency to cry about $this, stating it cannot be used as a lexical variable.

Is there a way, without passing it as an argument (any approach, use() or not, is likely fine) to accomplish this?

Off hand, the only thing I can think of would be something like:

 ... function($foo, $bar)
    use($array_with_this){
        extract($array_with_this); // contains instance with key 'this'

        // code using $this

    } ...

But this is forcing an additional requirement, which I’d rather not need.

  • 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-24T17:02:08+00:00Added an answer on May 24, 2026 at 5:02 pm
    $that = $this;
    cb(function () use ($that) {
        $that-> ...
    });
    

    This $this is a special keyword, you cannot use it outside of an object context. And anonymous functions currently run outside of the of the object context. This will change in the next version of PHP (5.4) apparently.

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

Sidebar

Related Questions

This may be the silliest question i have ever asked here, but i just
(This question may belong on a different site such as SuperUser. If so, please
This may not be explained the best but here it goes. I have a
I understand this is a subjective question but I want to see how others
If I understand this correctly: Current CPU developing companies like AMD and Intel have
I don't understand this: + (void)beginAnimations:(NSString *)animationID context:(void *)context (void *) ----> what kind
I know this is a stupid question, but I have been teaching myself c#,
This may seem like a daft question, but i was wondering about how to
This may sound a bit provocative but it actually is a real question. Feel
This may be opinionated or whatever, but I think its a fair question to

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.