Say I have these classes:
<?php
class Foo {
function test( $s='' ) {
return 'Foo test( '.$s.' )';
}
}
class Bar extends Foo {
function test( $s='' ) {
return 'Bar test( '.$s.' )';
}
}
$x = new Bar();
echo $x->test( 'hi' );
As expected, echo $x->test();
produces "Bar test( hi )"
Is there a shortcut to produce "Foo test( hi )" instead?
I tried things like echo $x->parent::test();
to no avail.
To be clear, I am aware I can add something like this inside Bar
:
function callParent( $method ) {
return parent::$method( ...array_slice( func_get_args(), 1 ) );
}
and then do this:
echo $x->callParent( 'test', 'hi' );
You can use reflection to call the parent's method implementation:
<?php
class Foo {
function test( $s='' ) {
return 'Foo test( '.$s.' )';
}
}
class Bar extends Foo {
function test( $s='' ) {
return 'Bar test( '.$s.' )';
}
}
$x = new Bar();
$reflection = new ReflectionObject($x);
$parent = $reflection->getParentClass();
$parentMethod = $parent->getMethod('test');
echo $parentMethod->invoke($x, 'hi');