Ask the subclass object to call directly the overloaded function within the base class?
The question looks like the following code
Class A
{
function A ()
{
$this->nnum = 1024;
}
function Selfclass ()
{
echo "Class A
";
}
}
Class B extends A
{
function Selfclass ()
{
echo "Class B
";
}
}
?>
$obj = new B ();
$obj->selfclass (); Call the function Selfclass () function of Class B
/* How can I use the Selfclass () function of the base class of object obj?
C + + can write similar code $obj->a::selfclass (), then how can PHP be implemented? */
?>
Share to:
------Solution--------------------
Reference: What
about parameter passing?
It's not that troublesome.
Class A
{
function A ()
{
$this->nnum = 1024;
}
function Selfclass ($a, $b, $c, $d, $e, $f)
{
echo "Class A
" . $this->nnum. "
" . " $a, $b, $c, $d, $e, $f ". "
";
}
function __call ($name, $arguments) {
List ($class _name, $function _name) = Explode (' _ ', $name);
if (is_subclass_of ($this, $class _name))
{
Reflect a method
$reflectionMethod = new Reflectionmethod ($class _name, $function _name);
Called with the current object
$reflectionMethod->invokeargs ($this, $arguments);
}
}
}
Class B extends A
{
function B ()
{
$this->nnum = 2048;
}
function Selfclass ()
{
echo "Class B
" . $this->nnum. "
";
}
}
Class C extends B
{
function C ()
{
$this->nnum = 4096;
}
function Selfclass ()
{
echo "Class C
" . $this->nnum. "
";
}
}
$obj = new B ();
$obj->selfclass (); Call the function Selfclass () function of Class B
$obj->a_selfclass (1,2,3,4,5,6,7);
$obj = new C ();
$obj->selfclass (); Call the function Selfclass () function of Class C
$obj->a_selfclass (1,2,3,4,5,6,7);
$obj->b_selfclass ();