A detailed explanation of the difference between overload and override in PHP

Source: Internet
Author: User
This paper mainly introduces the difference between overload and override in PHP. Have a good reference value, follow the small series together to see it

Override (override, overwrite)

1, method name, parameter, return value is the same.

2. The subclass method cannot reduce the access rights of the parent class method.

3. The subclass method cannot throw more exceptions than the parent class method (but the subclass method can not throw an exception).

4, exists between the parent class and the child class.

5. The method is defined as final and cannot be overridden.

Overload (heavy duty, overload)

1, the parameter type, number, order at least one is not the same.

2. You cannot overload a method name that has only a different return value.

3, exists in the parent class and subclass, in the same class.

The rewriting of methods (overriding) and overloads (overloading) are different manifestations of Java polymorphism.

Overrides (overriding) are a representation of the polymorphism between a parent class and a subclass, whereas overloading (overloading) is a representation of polymorphism in a class.

If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is called, and for it the definition in the parent class is "masked".

If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types or have different order of arguments, which is called a method overload (overloading). cannot be overloaded by Access permission, return type, thrown exception.

1. Override Features

1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;

2. The return value of the overridden method must be the same as the return of the overridden method;

3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;

4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

2.Overload Features

1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must differ, such as can be fun (int, float), but not fun (int, int));

2, can not be overloaded by access rights, return type, thrown exception;

3, the method of the exception type and number will not affect the overload;

4, for inheritance, if a method in the parent class is the access permission is PRIAVTE, then it can not be overloaded in the subclass, if defined, but also only defined a new method, but not to achieve the overloaded effect.

The above is the PHP overload and override the difference between the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.