My situation is best described with a bit of code:
class Foo {
function bar () {
echo "called Foo::bar()";
}
}
class SubFoo extends Foo {
function __call($func) {
if ($func == "bar") {
echo "intercepted bar()!";
}
}
}
$subFoo = new SubFoo();
// what actually happens:
$subFoo->bar(); // "called Foo:bar()"
// what would be nice:
$subFoo->bar(); // "intercepted bar()!"
I know I can get this to work by redefining bar()
(and all the other relevant methods) in the sub-class, but for my purposes, it'd be nice if the __call
function could handle them. It'd just make things a lot neater and more manageable.
Is this possible in PHP?
One thing you can try is to set your functions scope to private or protected. When one private function is called from outside the class it calls the __call magic method and you can exploit it.
__call()
is only invoked when the function isn't otherwise found so your example, as written, is not possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With