Q: How can a subclass object directly call a function that is overloaded in the base class? -Php Tutorial

Source: Internet
Author: User
Q: 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? */
?>


Reply to discussion (solution)

I'm not asking for a similar answer.
Class B extends
{
Function SelfClass ()
{
Echo "class B
";
}

Function SelfClassX ()
{
Parent: SelfClass ();
}
}

Php does not provide such syntax, but it can be simulated, but it is not recommended

 Nnum= 1024;} function SelfClass () {echo "class
". $ This-> nNum ."
";} Function _ call ($ name, $ arguments) {list ($ class_name, $ function_name) = explode ('_', $ name ); if (is_subclass_of ($ this, $ class_name) {if ($ class_name = _ CLASS _) {self: $ function_name ();} else {$ class_name :: $ function_name () ;}}} 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 SelfClass () function of Class B $ obj-> A_SelfClass (); $ obj = new C (); $ obj-> SelfClass (); // call the SelfClass () function of class C $ obj-> A_SelfClass (); $ obj-> B _SelfClass ();

This syntax $ class_name: $ function_name (); cannot be edited.
And the premise of self: $ function_name (); is that $ function_name () is a static function.

 Nnum= 1024;} function SelfClass () {echo "class
". $ This-> nNum ."
";} Function _ call ($ name, $ arguments) {list ($ class_name, $ function_name) = explode ('_', $ name ); if (method_exists ($ this, $ function_name) & is_a ($ this, $ class_name) {if (is_subclass_of ($ this, $ class_name) {:: $ function_name () ;}else {$ this-> $ function_name () ;}}} class B extends A {function B () {$ this-> nNum = 2048 ;} function SelfClass () {echo "class B
". $ This-> nNum ."
";}}$ Obj = new B (); $ obj-> B _SelfClass (); // call the SelfClass () function of Class B $ obj-> A_SelfClass (); // call the SelfClass () function of Class

I don't quite understand what it means
But at least 21 rows of A: $ function_name ();
$ Class_name: $ function_name ();
Otherwise, it will not be universal.

$ Class_name: $ function_name (); the syntax is incorrect. You cannot write it like this.
$ Class_name can only be parsed as a string and cannot be used. Your idea is beautiful, but it cannot be realized.

This is not just a good wish! Test results show that $ class_name: $ function_name () is correct (php5.4.12)

Note that:
Whether writing $ class_name: $ function_name ()
Or write A: $ function_name ()
Because $ function_name is a static method, but not

Here, php does not report an error, which is obviously a BUG.

My testing environment cannot be implemented for your proposed statement, and you cannot say so. I can pass the test here.
I have tested that A: $ function_name () is acceptable. this call does not require $ function_name to be A static method.
In addition, the $ class_name: $ function_name () method cannot be used to resolve $ class_name to a class.
Only strings can be parsed. Is my PHP version too old !!!

A: SelfClass (); // Strict Standards: Non-static method A: SelfClass () shocould not be called statically this is A normal syntax error (blocked)
$ Function_name = 'selfclass ';
A: $ function_name (); // It is not normal if no syntax error is reported here.

There should be no dual standards in a system!

My PHP Version is 5.2.6.

In A book, I found A: $ function_name (); A use case similar to this syntax.

This section is from PHP and MySQL programming 4th

Class name: method name is a long term, which is limited by php5.3.

I used C ++ before. when I learned PHP, I always thought that some usage would be similar.

C ++ is compiled and executed, so it doesn't matter if the source code is scanned several times.
Php is interpreted and executed. multiple scans will affect the execution efficiency.

If you write the interpreter on your own, you will find that many of the ambiguity problems cannot be solved during the second scan.

The moderator mentioned that the problem of ambiguity is indeed the focus. I want to ask the moderator, I used to develop C ++ games and want to change my website
As a PHP programmer, you do not need to learn javascript, but to what extent do you need to learn basic html.

Use reflection directly. you can test php5.2 and php5.3.

 Nnum= 1024;} function SelfClass () {echo "class
". $ This-> nNum ."
";} 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 ); // use the current object to call $ reflectionMethod-> invoke ($ this) ;}} 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 SelfClass () function of Class B $ obj-> A_SelfClass (); $ obj = new C (); $ obj-> SelfClass (); // call the SelfClass () function of class C $ obj-> A_SelfClass (); $ obj-> B _SelfClass ();

What about parameter transfer?

The launch method is a good idea, but the parameter issue is troublesome.

I guess we can use the evla () function to solve this problem.

It is wrong. it should be void eval (string code_str );

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 ();

Oh, it is indeed a good solution. Thanks to hnxxwyq

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.