PHP abstract classes, interfaces, and contractual Programming

Source: Internet
Author: User

Abstract class Yes Abstract Class declared by the keyword. By marking a class as an abstract class, you can Deferred implementation of the declared Method . To lifecycle a method as an abstract method, you only need to omit the method implementation body that contains all braces and declare Code End the row with a semicolon.
Abstract class Cannot be instantiated directly , They Must be inherited . If a class inherits from an abstract class, when it does not implement all the Abstract METHODS declared in the base class, it must be declared as abstract.
In Interface You can also declare a method prototype without a method body, which is similar to an abstract class. The difference between them is that, The interface cannot declare any method with the method body And they use different syntaxes. To force interface rules to a class, you must useImplements Keyword instead Extends Keyword.
In some cases, we want to determine whether a class is of a specific type or whether a specific interface is implemented. Instanceof The operator is ideal for this task. The instanceof operator checks three things: Whether the instance is of a specific type , Whether the instance inherits from a specific type , Whether the instance or any of its ancestor classes implement specific Interfaces .
Some languages have the ability to inherit from multiple base classes, which becomes multi-inheritance. PHP does not support multiple inheritance . Instead, it provides Declare multiple interfaces for a class .
The interface is very useful in declaring the rules that a class must follow. TitleConventional Programming Technology uses this feature to enhance encapsulation and optimize workflows.

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.