A summary of the PHP inheritance classes

Source: Internet
Author: User

1: Why do I need to inherit a class

In order to use a method of this class or to override the method of this class

The inheritance of a class is a method of inheriting the subclass to inherit the parent class.

Benefits of Inheritance: Subclasses can inherit the properties and methods of the parent class and allow methods or new methods to overwrite the parent class

The metaphor in nature is that "evolution" is the way to acquire new traits without affecting the old species.

2: Inherited syntax

Class Boy extends human{

}

3:final class and Final method

The final class cannot be inherited, and the final method cannot be overridden

Example

Class bb{

Final public Function cc () {

Echo ' Hongniu '

}

}

Class GG extends bb{

Public Function cc () {

Echo ' Dongpeng special drink '

}

}

$g = new BB ();

Echo $g;

If written in this way, the error will indicate that if you inherit a class if the property of the method is final, it cannot be inherited and overridden.

If the BB class is preceded by the final keyword then the statement class GG extends bb{} will also error stating if the class declaration

When the final keyword is written, the class cannot be inherited, and all methods of the class cannot be used and inherited.




A summary of the PHP inheritance classes

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.