PHP overloading is used in object-oriented classes, which is not the same as C + +, where overloads can be used for process-oriented and object-oriented, and the methods are different. In C + +, overloading applies to cases where the function name is the same but the number of arguments is different, and when the program compiles, the corresponding function is automatically selected based on the number of arguments provided, such as the following section of C + + code:
#include <iostream>using namespace Std;class box{private: int l,w;public: void Set () { l=0; w=0; }
The overload here is the set () function void set (int ll) { l=ll; w=0; } void set (int ll,int ww) { l=ll; w=ww; } void display () { cout<< "l=" <<l<< ";"; cout<< "w=" <<w<<endl; }; int main () { Box box_1,box_2,box_3; Box_1.set (); cout<< "box_1:\n"; Box_1.display (); Box_2.set (5); cout<< "box_2:\n"; Box_2.display (); Box_3.set (7,9); cout<< "box_3:\n"; Box_3.display ();}
The output is:
Box_1:
L=0;w=0
Box_2:
l=5; W=0
Box_3:
l=7; W=9 you can see from the results that different functions are executed when the number of parameters is different.
Back to the point, PHP overload in my opinion is actually a kind of overwrite (or modify), that is, on the basis of the parent class, modify, give the subclass a new function, the function is not in the parent class. This is also true in C + +, except that it is not overloaded in C + +.
Refer to the code:
<! DOCTYPE html>
Output Result:
I am a student!
I Haven ' t graduate
I was a student before!
But I had graduated!
Overloading of PHP, C + +