2

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

seyed
  • 1,555
  • 1
  • 17
  • 24

4 Answers4

0

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']();
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • 1
    It basically is the same in PHP 5.3, but the `[..]` short array syntax isn't available in PHP 5.3. – kokx Jan 07 '13 at 19:21
  • 1
    @kokx no `$fs['f1']();` not supported. and php say: `Call to undefined function \vendor\foo\Bar::f1() in ...` but `\vendor\foo\Bar::f1();` or `call_user_func($fs['f1']);` work correctly. – seyed Jan 07 '13 at 19:32
0

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'));

?>
Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
0

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
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
-1

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');
kokx
  • 1,706
  • 13
  • 19
  • when use `$fs['f1']();` php say: `Fatal error: Call to undefined function \vendor\foo\Bar::f1()`. `{$fs['f1']}();` has syntax error. – seyed Jan 07 '13 at 19:14
  • Ah, I made a mistake. Callables are defined a little differently. Will edit the answer accordingly. – kokx Jan 07 '13 at 19:19