Q: How can a subclass object directly call a function that is overloaded in the base class? /// & Nbsp; check the following code: & lt ;? Phpclass & nbsp; A {function & nbsp; A () {& nbsp; $ this-& gt; nNum & nbsp; = & nbsp; 102 How can a subclass object directly call a function that is overloaded in the base class?
/// Check the following code for the problem:
Class
{
Function ()
{
$ This-> nnum= 1024;
}
Function SelfClass ()
{
Echo "class
";
}
}
Class B extends
{
Function SelfClass ()
{
Echo "class B
";
}
}
?>
$ Obj = new B ();
$ Obj-> SelfClass (); // call the SelfClass () function of class B.
/* How can I use the SelfClass () function of the base class of the Object obj?
In C ++, you can write similar code $ obj-> A: SelfClass (). how can PHP be implemented? */
?> Share To: nNum? =? 1024;} function? SelfClass () {... 'data-pics = ''>
------ Solution --------------------
Reference:
What about parameter transfer?
Not that troublesome
Class
{
Function ()
{
$ This-> nnum= 1024;
}
Function SelfClass ($ a, $ B, $ c, $ d, $ e, $ f)
{
Echo "class
". $ 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 );
// Call with the current object
$ ReflectionMethod-> invokeArgs ($ this, $ arguments );
}
}
}
Class B extends
{
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 SelfClass () function of class B.
$ Obj-> A_SelfClass (1, 2, 3, 4, 5, 6, 7 );
$ Obj = new C ();
$ Obj-> SelfClass (); // call the SelfClass () function of class C.
$ Obj-> A_SelfClass (1, 2, 3, 4, 5, 6, 7 );
$ Obj-> B _SelfClass ();