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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:03:11+00:00 2026-05-15T08:03:11+00:00

Recently in a project I am working on, I needed to store callbacks in

  • 0

Recently in a project I am working on, I needed to store callbacks in a static member array, like so:

class Example {
    private static $_callbacks = array(
        'foo'=>array('Example','do_foo'),
        'bar'=>array('Example','do_bar')
    );
    private static function do_foo() { }
    private static function do_bar() { }
}

To call them, I tried the obvious (maybe even naive) syntax (inside the Example class):

public static function do_callbacks() {
    self::$_callbacks['foo']();
    self::$_callbacks['bar']();
}

To my surprise, this did not work, resulting in a notice that I was accessing an undefined variable, and a fatal error stating that self::$_callbacks['foo'] needed to be callable.

Then, I tried call_user_func:

public static function do_callbacks() {
    call_user_func(self::$_callbacks['foo']);
    call_user_func(self::$_callbacks['bar']);
}

And it worked!

My question is:

Why do I need to use call_user_func as an intermediary, and not directly call them?

  • 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-15T08:03:12+00:00Added an answer on May 15, 2026 at 8:03 am

    You can’t call callbacks by appending (). That only works in PHP 5.3 with lambda functions and objects that implement the __invoke magic (see also the internal get_closure object handler).

    First, despite what you say, this doesn’t work:

    <?php
    class Example {
        private static $_callbacks;
        private static function do_foo() { echo "foo"; }
        private static function do_bar() { echo "bar"; }
    
        public static function do_callbacks() {
            self::$_callbacks['foo'] = array('Example','do_foo');
            self::$_callbacks['bar'] = array('Example','do_bar');
    
            self::$_callbacks['foo']();
            self::$_callbacks['bar']();
        }
    }
    Example::do_callbacks();
    

    But it wouldn’t even work if self::$_callbacks['foo'] was a lambda:

    <?php
    class Example {
        private static $_callbacks;
    
        public static function do_callbacks() {
            self::$_callbacks['foo'] = function () { echo "foo"; };
    
            self::$_callbacks['foo']();
        }
    }
    
    Example::do_callbacks();
    

    The reason is the parser. The above compiles to:

    Class Example:
    Function do_callbacks:
    (...)
    number of ops:  16
    compiled vars:  !0 = $_callbacks
    line     # *  op                           fetch          ext  return  operands
    ---------------------------------------------------------------------------------
       6     0  >   EXT_NOP                                                  
       7     1      EXT_STMT                                                 
             2      ZEND_FETCH_CLASS                                         
             3      ZEND_DECLARE_LAMBDA_FUNCTION                             '%00%7Bclosure%7D%2Ftmp%2Fcp9aicow0xb7fcd09b'
             4      FETCH_W                      static member       $2      '_callbacks'
             5      ZEND_ASSIGN_DIM                                          $2, 'foo'
             6      ZEND_OP_DATA                                             ~3, $4
       9     7      EXT_STMT                                                 
             8      FETCH_DIM_R                                      $5      !0, 'foo'
             9      ZEND_FETCH_CLASS                                         
            10      ZEND_INIT_STATIC_METHOD_CALL                             $6, $5
            11      EXT_FCALL_BEGIN                                          
            12      DO_FCALL_BY_NAME                              0          
            13      EXT_FCALL_END                                            
      10    14      EXT_STMT                                                 
            15    > RETURN                                                   null
    

    There’s never a fetching of a static member (except for the assignment of the lambda). In fact, PHP compiles a variable $_callbacks, which turns out not to exist at runtime; hence your error. I concede this is, maybe not a bug, but at least a corner case of the parser. It evaluates the $_callbacks['foo'] part first and then attempts to call the static function whose name results from that evaluation.

    In sum – stick to call_user_func or forward_static_call.

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

Sidebar

Related Questions

I'm working on a c++ project, and we recently needed to include a small
So recently on a project I'm working on, we've been struggling to keep a
I've been working on an embedded C/C++ project recently using the shell in Tornado
I've recently installed VS2008. The project I'm working on uses vstest and I have
I recently was working with a subversion project that checked out code not only
I have recently started working on a very large C++ project that, after completing
I have recently been working with someone on a project that is very ajax
I am working on a legacy project in VC++/Win32/MFC . Recently it became a
I was recently working on a piece of C++ code for a side project
I've recently been writing some code for a research project that I'm working on,

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.