4

Say I have a static object:

class everything{
    public static function must(){
        return "go!";
    }
}

When I do this:

echo everything::must();

I get the response:

go!

Which is the expected behavior.


Now, for my own reasons (legacy code support) I'd like to be able to call that static object from the return of a function call, in a syntax similar to this:

print accessorFunction()::must(); // Or something as close to it as possible

function accessorFunction(){
    returns (reference to)everything; // Or something as close to it as possible
}

I hope I've made the question clear enough.

Thanks.

4

4 回答 4

1

您还可以使用变量类:

function accessorFunction() {
    return new everything();
}

$class = accessorFunction();
echo $class::must(); // go!
于 2013-10-01T20:51:32.467 回答
1

我不确定这是否是您正在寻找的那种参考,但您总是可以这样做:

print call_user_func( array( accessorFunction(), "must"));

function accessorFunction(){
    return 'everything';
}
于 2013-10-01T20:46:57.023 回答
1

不能以这种方式调用静态方法:

print accessorFunction()::must();

但可能

$class_name = accessorFunction();
print $class_name::must();

文档 - http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

于 2013-10-01T20:59:14.700 回答
1

我认为更清洁的选择是混合@nickb 和@GeorgeBrighton 解决方案:

function accessorFunction() {
    return 'everything';
}

$class = accessorFunction();
echo $class::must(); // prints go!
于 2013-10-01T21:01:25.870 回答