Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable as static function

Tags:

php

I have a class Sections:

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

And I would like to call the static function get() 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: Call to undefined function Sections::get()

Is it possible to call a static function in this way?

Thanks!

like image 244
Jan-Paul Kleemans Avatar asked Mar 21 '26 15:03

Jan-Paul Kleemans


1 Answers

You need to store the class separately from the method:

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

Now you can call it like this:

$class::$method('body');
like image 179
hek2mgl Avatar answered Mar 24 '26 03:03

hek2mgl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!