Reputation: 1044
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.
Upvotes: 4
Views: 144
Reputation: 1944
I think the cleaner option would be to mix @nickb and @GeorgeBrighton solutions:
function accessorFunction() {
return 'everything';
}
$class = accessorFunction();
echo $class::must(); // prints go!
Upvotes: 1
Reputation: 46
It's NOT possible to call static methods this way:
print accessorFunction()::must();
But possible
$class_name = accessorFunction();
print $class_name::must();
Documentation - http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
Upvotes: 1
Reputation: 5151
You could also use variable classes:
function accessorFunction() {
return new everything();
}
$class = accessorFunction();
echo $class::must(); // go!
Upvotes: 1
Reputation: 59709
I'm not sure if this is the kind of reference you're looking for, but you can always do:
print call_user_func( array( accessorFunction(), "must"));
function accessorFunction(){
return 'everything';
}
Upvotes: 1