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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:01:11+00:00 2026-06-05T06:01:11+00:00

The following code is a simplified example of what I’m trying to understand. I’m

  • 0

The following code is a simplified example of what I’m trying to understand.

I’m using an external library that uses callbacks to process multiple requests. Ultimately I’ve been trying to figure out how to make Test->inc() call ExternalLib for each array element, then wait for all callbacks to be executed before continuing.

As you can see, the fatal error on line 18 is due to the method being called via call_user_func. How can I do this, or is there perhaps a better method?

class Test {
    public $a = array();

    public function inc(array $ints){
        $request = new ExternalLib();
        foreach ($ints as $int) {
            $request->increment($int);
        }

        while( count($this->a) < count($ints) ) {
            usleep(500000);
        }

        $test->dump();
    }

    public function incCallback($in, $out) {
        /* append data to Test class array */
        $this->a[] = array($in => out); /* Fatal error: Using $this when not in object context */
    }

    public function dump() {
        /* Print to screen */
        var_dump($this->a);
    }
}


class ExternalLib {
    /* This library's code should not be altered */
    public function increment($num) {
        call_user_func(array('Test','incCallback'), $num, $num++);
   }
}


$test = new Test();
$test->inc(array(5,6,9));

Desired output:

array(3) {
  [5]=>
  int(6)
  [6]=>
  int(7)
  [9]=>
  int(10)
}

This code also available at codepad

  • 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-05T06:01:13+00:00Added an answer on June 5, 2026 at 6:01 am

    The problem isn’t a timing/waiting issue. It’s a static vs. instantiated issue.

    Calling the function using call_user_func(array('Test','incCallback')... is the same as calling Test::incCallback. You can’t use $this when making a static call.

    You will either need to modify the external lib to use an instantiated object or modify the Test class to use all static data.

    Edit

    I don’t know exactly what you’re looking to accomplish, but if making the class operate as a static class is your only option, then that’s what you have to do …

    There are a couple other issues with your code:

    • Based on your desired output, you don’t want a[] = array($in, $out) but rather a[$in] = $out
    • $num++ will not increment until after the function is called … you want ++$num

    Here is a working example …

    class Test {
        public static $a;
    
        public function inc(array $ints){
            $request = new ExternalLib();
            foreach ($ints as $int) {
                $request->incriment($int);
            }
    
            while( count(self::$a) < count($ints) ) {}
    
            self::dump();
        }
    
        public function incCallback($in, $out) {
            /* append data to Test class array */
            self::$a[$in] = $out;
        }
    
        public function dump() {
            /* Print to screen */
            var_dump(self::$a);
        }
    }
    
    
    class ExternalLib {
        /* This library's code should not be altered */
        public function incriment($num) {
            call_user_func(array('Test','incCallback'), $num, ++$num);
       }
    }
    
    
    Test::$a = array();
    Test::inc(array(5,6,9));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code (simplified example, that triggers the error) doesn't compile with VS 2008:
I've got following (simplified for example purpose) code and it works: void log(const string
I'm using gcc 4.3.2. I have the following code (simplified): #include <cstdlib> template<int SIZE>
I have the following code (asp.net-mvc, jquery) (i have simplified the example to show
Consider the following code sample that uses MEF to create an object of type
This is a simplified example, but I am working on a code translator that
Imagine I have the following code (simplified regarding my real context of course): <div
Following simplified code snippet: #include <QtGui> int main(int argc, char **argv) { QApplication app(argc,
The following code is a simplified version of what I use for event dispatching.
I have the following code (simplified): ostringstream oss; oss << Text ; oss <<

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.