The use of abstract methods, abstract classes, and interfaces in PHP

Source: Internet
Author: User

In a class, a method without a method body is an abstract method. Abstract Visibility Function Method name (parameter 1,.....);            If visibility is not specified, the default is public, such as public function Hello ($args), and abstract function work (); Modifier abstract, can also omit
abstract class class name {Properties        Method abstract method;}
features of abstract classes:
      Abstract classes cannot be instantiated and can only be inherited.
    • Abstract classes do not necessarily have abstract methods, classes with abstract methods, must be abstract classes.
    • The visibility of an abstract method cannot be private
    • Abstract methods in subclasses, need to be overridden.
When do I need to use abstract classes?
    • There is a method, the method body does not know how to write, subclasses must also have this method, encapsulated into an abstract method, the class is an abstract class.
    • You can use abstract methods when you must encapsulate certain methods in a control subclass.
    • When the required control class can only be inherited and cannot be instantiated.
Example: Declaring a human, there is an abstract way to work.    Declare a PHP instructor class and rewrite the method to work.        Abstract class people{protected $age = 22;        Public $height = 1.70;    Abstract function work (); } class Phpteacher extends people{function work () {}}if all methods in a class are abstract and have no member properties, then this class is called an interface (interface).
Interface common{abstract function work (); Abstract function test ($args);}
The function of the interface: Although the class of PHP is single-inheritance, it can be implemented through interfaces to achieve multiple inheritance.
Interface Inheritance (extends):
        Interface Inheritance Interface
Interface Interface name extends parent interface nameNote: The inheritance of a class is a single inheritance (only one parent), but the inheritance of the interface is multiple inheritance, and the implementation of the class to the interface is also multi-implemented.
Interface implementation (implements):
    • Class implements interface class name implements Interface Name 1, interface Name 2, ...
Inheriting classes implement interfaces at the same time:
    • The class inherits the parent class and implements the interface class name extends the parent class name implements interface name
    An
      interface is a special abstract class that contains only abstract methods and no member properties.
    • Class implementation (implements) interface, you must fully implement all the methods in the interface, and class inheritance (extends) abstract classes only need to override the abstract methods that are needed.
    • Abstract classes can only be inherited, but interfaces are multi-inheritance, and class-to-interface implementations are also multiple implementations.

The use of abstract methods, abstract classes, and interfaces in PHP

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.