Question

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

Was it helpful?

Solution

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

OTHER TIPS

I don't remember if this is supported in PHP 5.3, but you can do this in 5.4:

<?php
namespace Vendor;

class Foo
{
  public static function bar()
  {
    echo "bar\n";
  }
}

$funcs = [
  'bar' => ['\Vendor\Foo', 'bar']
];

$funcs['bar']();

In PHP 5.3 it depends on the type of callback being used. The example you gave, where it is a method of an object, cannot be invoked in the manner described. If the example had been a procedural function you could invoke it using the code you gave.

I'm not sure why this is the case from a technical understanding but my guess is that the PHP parser looks for a function named \vendor\foo\Bar::f1 and it can't find one. If you wish to invoke a variable function, i.e. $var(), then $var must be a function and not an object method. If you wish to call a variable method then check out the examples below.


The following examples are valid ways to invoke variable static object methods:

<?php

class Foo {

    public static function a() {
        echo 'Foo::a()';
    }

    public static function b() {
        echo 'Foo::b()';
    }

}


$foo = 'Foo';
$aFunc = 'a';
$bFunc = 'b';

$foo::$aFunc();
Foo::$bFunc();
call_user_func('Foo::' . $aFunc);
call_user_func(array($foo, 'b'));

?>

Yes, this is possible. But the way you are trying won't work. You have to use a callable:

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

$fs['f1']();
$fs['f2']('some id');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top