Overload (different from overwriting) is not supported in PHP. in OOP, you can overload a method to implement two or more methods with the same name, but there are different numbers or types of parameters (depending on the language), PHP is a loose type language, so through the type
Overload (different from overwriting) is not supported in PHP. in OOP, you can overload a method to implement two or more methods with the same name, but there are different numbers or types of parameters (depending on the language). PHP is a loose type language, so the type overload does not work, however, the number of parameters does not work.
Sometimes the overload constructor in OOP is very good, so that you can create objects using different methods (passing different numbers of parameters ). The tips for implementing it in PHP are:
- Class Myclass {
- Function Myclass (){
- $ Name = "Myclass". func_num_args ();
- $ This-> $ name ();
- // Pay attention to $ this-> $ name () is generally incorrect, but here $ name is the name of the method to be called
- }
- Function Myclass1 ($ x ){
- Code;
- }
- Function Myclass2 ($ x, $ y ){
- Code;
- }
- }
Through additional processing in the class, using this class is transparent to users:
- $ Obj1 = new Myclass ('1'); // call Myclass1
- $ Obj2 = new Myclass ('1', '2'); // call Myclass2
Sometimes this is very useful.