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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:13:33+00:00 2026-06-17T01:13:33+00:00

is possible get reference to static function and run? like this: namespace vendor\foo; class

  • 0

is possible get reference to static function and run? like this:

namespace vendor\foo;

class Bar
{
    public static function f1()
    {
        echo 'f1';
    }
    public static function f2($id)
    {
        echo 'f2: '.$id;
    }

}

and

$fs = array(
    'f1'=>\vendor\foo\Bar::f1,
    'f2'=>\vendor\foo\Bar::f2
);

$fs['f1']();
$fs['f2']('some id');

or the only way is call_user_func ?

note: 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-06-17T01:13:35+00:00Added an answer on June 17, 2026 at 1:13 am

    You have multiple options to do this

    1. using a string variable for the class name and the method name
    2. using callbacks together with call_user_func()
    3. using Reflection

    The following examples shall demonstrate these options:

    <?php
    
    namespace vendor\foo;
    
    class Bar {
    
        public static function foo($arg) {
            return 'foo ' . $arg;
        }   
    }
    

    Option 1 : Using a string variable for the classname and the method name:

    /* prepare class name and method name as string */
    $class = '\vendor\foo\Bar';
    $method = 'foo';
    // call the method
    echo $class::$method('test'), PHP_EOL;
    // output : foo test
    

    Option 2 : Perpare a callback variable and pass it to call_user_func() :

    /* use a callback to call the method */
    $method = array (
        '\vendor\foo\Bar', // using a classname (string) will tell call_user_func()
                           // to call the method statically
        'foo'
    );
    
    // call the method with call_user_func()
    echo call_user_func($method, 'test'), PHP_EOL;
    // output : foo test
    

    Option 3 : Use ReflectionMethod::invoke() :

    /* using reflection to call the method */
    $method = new \ReflectionMethod('\vendor\foo\Bar', 'foo');
    
    // Note `NULL` as the first param to `ReflectionMethod::invoke` for a static call.
    echo $method->invoke(NULL, 'test'), PHP_EOL;
    // output : foo test
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Resolving a linker error: undefined reference to static class members This declaration:
I have a static class like this: public static class MyApp { public static
I'd like to know whether it is possible to get a reference to the
I Have this error message : Possible unitended reference comparison; to get a value
Is it possible to reference a control in an application from a static function?
Possible Duplicate: Can I get a reference to the 'owner' class during the init
Is it possible to get a reference to the triggerElement that invoked the Ajax
Is it possible to get a reference to Swing no Drag Cursor ,since it
Possible Duplicate: How can I get a reference to a node directly after it
Possible Duplicate: undefined reference to static member variable What is an undefined reference/unresolved external

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.