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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:59:23+00:00 2026-06-15T20:59:23+00:00

I have a PHP code that looks like this: class A { public function

  • 0

I have a PHP code that looks like this:

class A {
    public function __construct() {
        $this->b = new B(function($x) { return $x + 1; });
    }
};

class B {
    public function __construct($dataProcessingFunction) {
        $this->dataProcessingFunction = $dataProcessingFunction;
    }

    public function processData($data) {
        $f = $this->dataProcessingFunction;
        return $f($data);
    }
};

But there is a problem: I absolutely need B’s destructor to be called before A’s destructor. This seems reasonable as you can see. The B object doesn’t need any A, so there should be no problem.

But since PHP 5.4.0, closures seem to automatically capture implicitly $this. Therefore, the lambda function that I pass to B and that is stored by B contains a reference to A.

Which means that A contains a pointer to B, and B contains a pointer to A (through the closure). In this kind of situation, the PHP documentation says that destructors are only called on garbage collection and in a random order. And guess what: B’s destructor is always called before A’s.

Is there a way to solve this in a elegant way?

  • 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-15T20:59:25+00:00Added an answer on June 15, 2026 at 8:59 pm

    Thanks to Explosion Pills, I’ve found the solution in the Closure class.

    You can in fact change the $this stored inside the closure, like this:

    $cb = function($x) { return $x + 1; };
    $cb = $cb->bindTo(null);
    
    // now $cb doesn't contain a pointer to $this anymore
    

    Note that you can’t do this, or you’ll get a syntax error:

    // syntax error
    $cb = (function($x) { return $x + 1; })->bindTo(null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP class that looks like this: class userAuthentication { public static
I have a line of php code that looks like this: echo <script>$('#edit_errors').html('<h3><em>Please Correct
I have code in cake php that looks like: public $ceff_instance = array(); public
I have an Entity class that looks like this: <?php namespace Entities; /** @Entity
I have a block of PHP code that looks like this: $flag = false;
So I have some PHP code that looks like: $message = 'Here is the
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have an abstract class where source code looks like this: /* * @assert
Possible Duplicate: PHP call class in class returns error:500 I have code that looks
I have a url that look like this reg.php?lang=no_NO&passkey=test and im trying to get

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.