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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:04:06+00:00 2026-06-13T05:04:06+00:00

I have two variables: $a = ‘some_class’; $b = ‘some_method’; What I want to

  • 0

I have two variables:

$a = 'some_class';
$b = 'some_method';

What I want to do is something like this (the method is static):

$a::$b;

Is it possible? I’ve tried the reflection class, but I can’t call static methods…

  • 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-13T05:04:07+00:00Added an answer on June 13, 2026 at 5:04 am

    How to invoke the static method, 3 options

    You have a few options:

    Code

    <?PHP
    
        class test {
            static function doThis($arg) {
                echo '<br>hello world '.$arg;
            }
        }
    
        $class='test';
        $method='doThis';
        $arg='stack';
    
        //just call
        $class::$method($arg);
    
        //with function
        call_user_func(array($class, $method), $arg);
    
        //ugly but possible
        $command=$class.'::'.$method.'("'.$arg.'");';
        eval($command);
    

    Output

        hello world stack
        hello world stack
        hello world stack
    

    What happens when you call it with those options

    Code with a backtrace so you can see what happens under the hood in PHP:

    Code

    <?PHP
    
    class test {
        static function doThis($arg) {
            echo 'hello world with argument: '.$arg.PHP_EOL;
            print_R(debug_backtrace());
        }
    }
    
    function runTest() {
    
        $class='test';
        $method='doThis';
        $arg='stack';
    
    
    
        //just call
        $class::$method($arg);
    
        //with function
        call_user_func(array($class, $method), $arg);
    
        //ugly but possible
        $command=$class.'::'.$method.'("'.$arg.'");';
        eval($command);
    
    }
    
    echo '<pre>';
    runTest();
    

    Output

    $class::$method($arg);

    hello world with argument: stack
    Array
    (
        [0] => Array
            (
                [file] => folder/test.php
                [line] => 19
                [function] => doThis
                [class] => test
                [type] => ::
                [args] => Array
                    (
                        [0] => stack
                    )
    
            )
    
        [1] => Array
            (
                [file] => folder/test.php
                [line] => 31
                [function] => runTest
                [args] => Array
                    (
                    )
    
            )
    
    )
    

    call_user_func(array($class, $method), $arg);

    hello world with argument: stack
    Array
    (
        [0] => Array
            (
                [function] => doThis
                [class] => test
                [type] => ::
                [args] => Array
                    (
                        [0] => stack
                    )
    
            )
    
        [1] => Array
            (
                [file] => folder/test.php
                [line] => 22
                [function] => call_user_func
                [args] => Array
                    (
                        [0] => Array
                            (
                                [0] => test
                                [1] => doThis
                            )
    
                        [1] => stack
                    )
    
            )
    
        [2] => Array
            (
                [file] => folder/test.php
                [line] => 31
                [function] => runTest
                [args] => Array
                    (
                    )
    
            )
    
    )
    

    eval($command);

    hello world with argument: stack
    Array
    (
        [0] => Array
            (
                [file] => folder/test.php(26) : eval()d code
                [line] => 1
                [function] => doThis
                [class] => test
                [type] => ::
                [args] => Array
                    (
                        [0] => stack
                    )
    
            )
    
        [1] => Array
            (
                [file] => folder/test.php
                [line] => 26
                [function] => eval
            )
    
        [2] => Array
            (
                [file] => folder/test.php
                [line] => 31
                [function] => runTest
                [args] => Array
                    (
                    )
    
            )
    
    )
    

    As you can see first way has no step in between which is being registered, it directly makes the call while the other 2 options act by themselves as a function and make the call from themselves.

    In practice not a lot of difference but it might make sense when optimizing such a process.

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

Sidebar

Related Questions

I have two variables, work.dir and my.file . This means the user would like
I have two variables, key and value , and I want to add them
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
In C++, if you have two variables a and b, you can do this
I have two environment variables in apache config, I want to concatenate them into
I have two variables like Question_type and Answer_type in JavaScript. I have a hidden_field
I have two variables in type of DateTime, and I want to sum them,
In java i have two variables String string; int integerValue; I want to convert
I have two variables, 'a' and 'b' in my JavaScript, and i want to
I have two variables, one with text, and another with patterns. And I want

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.