PHP inheritance in the application of method overloading (overwrite), PHP overload _php Tutorial

Source: Internet
Author: User

PHP inheritance in the application of method overloading (overwrite), PHP overload


This paper analyzes the application of method overloading (overwriting) in PHP inheritance. Share to everyone for your reference. The specific analysis is as follows:

Method overloading (override)/overwrite--When to use: When the parent knows all of the subclasses need a method, but the parent does not know how to write this method, it is necessary to use the overloaded method. At this point, the subclass can be overridden to override this method.

Popular example-The parent class (animal) knows that its subclasses (cats and dogs) will be called, but they are not the same, so the parent can't write this method, only the subclass (cat and dog) to define. The code is as follows:

<?phpclass animal{public $name; protected $price;  Function Cry () {echo "does not know what animals call";}} Class Dog extends animal{function cry () {echo "Barking ...";}} Class Pig extends animal{function cry () {echo "Hem ..."}}? >

Method overloading/overriding points and details (read it a little):

1, the method that requires the subclass, its method name, parameter list and the parent class method is identical. As in the example cry (), can not add parameters, change to Cry ($NAAA) and so on. Note: The parameter name is not required here, just the number of parameters required is the same.
2. The so-called overloads or overrides do not override the methods of the parent class. It can also be understood that if the subclass has called the method, if the method is not found in its own class, then it will go to the parent class to see if there is this method. It's also a polymorphic understanding.
3. If a method in a subclass inherits the contents of the same name method in the parent class, you can use the Parent:: Method name or parent class Name:: Method name inheritance. Used within a method defined by a subclass.
4. Access rights problem, the subclass's access scope >= the access scope of the parent class, that is, if the parent class is protected function cry (), the subclass can only be protected or public.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/955401.html www.bkjia.com true http://www.bkjia.com/PHPjc/955401.html techarticle PHP inheritance in the application of method overloading (overwrite), PHP overload This article analyses the application of method overloading (overwrite) in PHP inheritance. Share to everyone for your reference. Specific analysis ...

  • 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.