This article mainly introduces the difference between overload and override in PHP. It has a very good reference value. Let’s take a look at it with the editor.
override (rewrite, override)
1. Method name, parameters, return value same.
2. Subclass methods cannot reduce the access rights of parent class methods.
3. Subclass methods cannot throw more exceptions than parent class methods (but subclass methods do not need to throw exceptions).
4. Exists between parent class and subclass.
5. Methods are defined as final and cannot be overridden.
overload (overload, overload)
1. At least one of the parameter type, number, and order is different.
2. Method names with different return values cannot be overloaded.
3. Exists in parent class, subclass, and same class.
Method overriding (Overriding) and overloading (Overloading) are different manifestations of Java polymorphism.
Overriding is a manifestation of polymorphism between a parent class and a subclass, and overloading is a manifestation of polymorphism in a class.
If a method defined in a subclass has the same name and parameters as its parent class, we say that the method is overriding (Overriding). When an object of a subclass uses this method, the definition in the subclass will be called. For it, the definition in the parent class is like being "shielded".
If multiple definitions with the same name are defined in a class Methods that may have different numbers of parameters, different parameter types, or different parameter orders are called method overloading. It cannot be overloaded through access permissions, return types, and thrown exceptions.
1. Override characteristics
1. The flag of the overridden method must be and be overridden The overriding effect can only be achieved by completely matching the flags of the method;
2. The return value of the overridden method must be consistent with the return value of the overridden method;
3. The overridden method throws The exception thrown must be consistent with the exception thrown by the overridden method, or its subclass;
4. The overridden method cannot be private, otherwise it will just define a new method in its subclass , and it is not covered.
2. Overload Features
1. When using overload, you can only use different parameter styles. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types in the same method must be different, for example, it can be fun(int, float), but it cannot be fun(int, int ));
2. It cannot be overloaded by access permissions, return types, and thrown exceptions;
3. The exception type and number of methods will not affect overloading;
4. For inheritance, if a method has privileged access in the parent class, it cannot be overloaded in the subclass. If it is defined, it will only define a new method. It will not achieve the effect of overloading.
The above is a detailed explanation of the difference between overload and override in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!