Variable as static function

I have a class Sections:

class Sections {
    public static function get($name) {
        //
    }
}

      

And I would like to call a static get () function from it with a variable ( http://php.net/manual/en/functions.variable-functions.php ):

$section = 'Sections::get';
$section('body');

      

But it gives me a fatal error: function call undefined Sections :: get ()

Can a static function be called this way?

Thank!

+3


source to share


3 answers


You need to keep the class separate from the method:

$class = 'Sections';
$method = 'get';

      



Now you can call it like this:

$class::$method('body');

      

+4


source


Try to use call_user_func

for this:

$section = array('Sections', 'get');
call_user_func($section, 'body');

      



Or:

$section = 'Sections::get';
call_user_func($section, 'body');

      

0


source


I solved it with a function outside the class:

class Sections {
    public static function get($name) {
        //
    }
}

function section($name) {
    Sections::get($name);
}

      

Now I can do:

$section = 'section';
$section('body');

      

0


source







All Articles