This section describes how to use a subclass object to directly call functions that are overloaded in the base class.

Source: Internet
Author: User
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.