Can a PHP function return a reference to a static object?

Say I have a static object:

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

When I do this:

echo everything::must();

I get the answer:

go!

What is the expected behavior.


Now, for my own reasons (legacy code support), I would like to be able to call this static object from the return of a function call in syntax like 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 have a fairly clear-cut question.

Thanks.

+4
source share
4 answers

It is not possible to call static methods as follows:

print accessorFunction()::must();

But probably

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

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

+1
source

, , , :

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

function accessorFunction(){
    return 'everything';
}
+1

:

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

$class = accessorFunction();
echo $class::must(); // go!
+1

, @nickb @GeorgeBrighton:

function accessorFunction() {
    return 'everything';
}

$class = accessorFunction();
echo $class::must(); // prints go!
+1

All Articles