[ACCEPTED]-Reference to static method in PHP?-syntax

Accepted answer
Score: 30

PHP handles callbacks as strings, not function 8 pointers. The reason your first test works 7 is because the PHP interpreter assumes foo1 as 6 a string. If you have E_NOTICE level error 5 enabled, you should see proof of that.

"Use 4 of undefined constant foo1 - assumed 'foo1'"

You 3 can't call static methods this way, unfortunately. The 2 scope (class) is relevant so you need to 1 use call_user_func instead.

<?php

function foo1($a,$b) { return $a/$b; }

class Bar
{
    public static function foo2($a,$b) { return $a/$b; }

    public function UseReferences()
    {
        $fn = 'foo1';
        echo $fn(6,3);

        $fn = array( 'self', 'foo2' );
        print call_user_func( $fn, 6, 2 );
     }
}

$b = new Bar;
$b->UseReferences();
Score: 10

In php 5.2, you can use a variable as the 5 method name in a static call, but to use 4 a variable as the class name, you'll have 3 to use callbacks as described by BaileyP.

However, from 2 php 5.3, you can use a variable as the class 1 name in a static call. So:

class Bar
{
    public static function foo2($a,$b) { return $a/$b; }

    public function UseReferences()
    {
        $method = 'foo2';
        print Bar::$method(6,2); // works in php 5.2.6

        $class = 'Bar';
        print $class::$method(6,2); // works in php 5.3
     }
}

$b = new Bar;
$b->UseReferences();
?>
Score: 6

You could use the full name of static method, including 3 the namespace.

<?php
    function foo($method)
    {
        return $method('argument');
    }

    foo('YourClass::staticMethod');
    foo('Namespace\YourClass::staticMethod');

The name array array('YourClass', 'staticMethod') is equal to 2 it. But I think the string may be more clear 1 for reading.

Score: 1

In PHP 5.3.0, you could also do the following:

<?php

class Foo {
    static function Bar($a, $b) {
        if ($a == $b)
            return 0;

        return ($a < $b) ? -1 : 1;
    }
    function RBar($a, $b) {
        if ($a == $b)
            return 0;

        return ($a < $b) ? 1 : -1;
    }
}

$vals = array(3,2,6,4,1);
$cmpFunc = array('Foo', 'Bar');
usort($vals, $cmpFunc);

// This would also work:
$fooInstance = new Foo();
$cmpFunc = array('fooInstance', 'RBar');
// Or
// $cmpFunc = array('fooInstance', 'Bar');
usort($vals, $cmpFunc);

?>

0

Score: 1

Coming from a javascript background and 8 being spoiled by it, I just coded this:

function staticFunctionReference($name)
{
    return function() use ($name)
    {
        $className = strstr($name, '::', true);
        if (class_exists(__NAMESPACE__."\\$className")) $name = __NAMESPACE__."\\$name";
        return call_user_func_array($name, func_get_args());
    };
}

To 7 use it:

$foo = staticFunctionReference('Foo::bar');
$foo('some', 'parameters');

It's a function that returns a function 6 that calls the function you wanted to call. Sounds 5 fancy but as you can see in practice it's 4 piece of cake.

Works with namespaces and 3 the returned function should work just like 2 the static method - parameters work the 1 same.

Score: 0

This seems to work for me:

<?php

class Foo{
    static function Calc($x,$y){
        return $x + $y;
    }

    public function Test(){
        $z = self::Calc(3,4);

        echo("z = ".$z);
    }
}

$foo = new Foo();
$foo->Test();

?>

0

Score: 0

In addition to what was said you can also 1 use PHP's reflection capabilities:

class Bar {

    public static function foo($foo, $bar) {
        return $foo . ' ' . $bar;
    }


    public function useReferences () {
        $method = new ReflectionMethod($this, 'foo');
        // Note NULL as the first argument for a static call
        $result = $method->invoke(NULL, '123', 'xyz');
    }

}

More Related questions