PHP Using a variable when calling a static method

后端 未结 5 639
故里飘歌
故里飘歌 2021-01-14 01:55

I have three classes that all have a static function called \'create\'. I would like to call the appropriate function dynamically based on the output from a form, but am hav

5条回答
  •  独厮守ぢ
    2021-01-14 02:48

    use call_user_func

    heres an example from php.net

    class myclass {
        static function say_hello()
        {
            echo "Hello!\n";
        }
    }
    
    $classname = "myclass";
    
    call_user_func(array($classname, 'say_hello'));
    call_user_func($classname .'::say_hello'); // As of 5.2.3
    
    $myobject = new myclass();
    
    call_user_func(array($myobject, 'say_hello'));
    

提交回复
热议问题