Reputation: 3338
So I know I can use __callStatic() to allow for:
Example::test();
Too work... But what I was wondering if its possible to overload
Example::test->one()
Example::test->two()
Is this possible at all with PHP? If so does anyone have a working example?
Or does anyone know of a work around?
Edit
Thought I would add some more details on why am wanting to do this and its basically cause I have a API with has methods like api.method.function and want to build a simple script to interact with that api.
There is also some methods that are just api.method
Upvotes: 0
Views: 113
Reputation: 9172
To quote the manual:
__callStatic() is triggered when invoking inaccessible methods in a static context.
So it doesn't work with properties - what you would need for your example to work as you described it is __getStatic
. But, that doesn't exist.
The closest workaround that I can think of would be to use something like:
Example::test()->one();
Example::test()->two();
Where test()
can be defined through __callStatic
and it returns an object which has the methods one
and two
, or has __call
defined.
EDIT:
Here's a small example (code hasn't been tested):
class ExampleTest {
public function __call($name, $arguments) {
if ($name == 'one') {
return 'one';
}
if ($name == 'two') {
return 'two';
}
}
}
class Example {
public static function __callStatic($name, $arguments) {
if ($name == 'test') {
// alternatively it could be return ExampleTest.getInstance() if you always want the same instance
return new ExampleTest();
}
}
}
Upvotes: 4