[PHP] abstract classes and interfaces

Source: Internet
Author: User
[PHP] abstract classes and interface abstract classes are special classes. interfaces are special abstract classes, and polymorphism requires the use of abstract classes or interfaces.

Abstract class what is an abstract method?

Definition: in a class, a method without a method body is an abstract method (that is, a method does not use {} but ends with a semicolon)

Abstract function test (); // abstract method function test () {// method body, but the method body is empty}

If a method is an abstract method, abstract modification must be used. why should abstract methods be used?

What is an abstract class?
  1. If a class has an abstract method, the class is an abstract class.

  2. If a class is an abstract class, the class must be modified using abstract.

  3. An abstract class is a special class because there are abstract methods in a class and others remain unchanged. You can also declare member attributes, constants, and non-abstract methods in abstract classes.

  4. An abstract class cannot instantiate an object (an object of an abstract class cannot be created through an abstract class)

// Common class name {}// abstract class name {}abstract class Demo {var a; abstract function fun1 (); function fun2 (){}}

Abstract class:
  1. To use an abstract class, you must use a class to inherit the abstract class. to use this subclass, that is, to allow the subclass to create objects, the subclass must no longer be an abstract class, subclass can override the method of the parent class (add the method body to the abstract method ). The method in the abstract method does not have a method body, and the subclass must implement this method (the parent class does not write a specific implementation, but the subclass must have this method name)

  2. Columns use abstract classes to define certain specifications so that child classes can implement their own functions according to these specifications.

Interface
  • An interface is a special abstract class, and an abstract class is a special class.

  • An interface has the same effect as an abstract class.

  • Abstract classes provide specific implementation standards, while interfaces are pure templates. The interface only defines the function and does not contain the implemented content. The interface is declared with the keyword interface.

  • Because PHP is a single inheritance, if an abstract class is used, the subclass cannot inherit other classes after the abstract class is implemented. If you want to implement some specifications and inherit from another class. Interface is required

Declaration method

Interface name {const constant name = constant value; // The attribute in the interface must be the constant function name ();} echo interface name: constant name; interface Demo {const HOST = "localhost"; function fun () ;}echodemo: HOST; // The output is localhost

Comparison between interfaces and abstract classes
  1. Objects cannot be created for the same purpose. all objects must be implemented by sub-classes.

  2. The interface description is different from the abstract class.

  3. The interface is implemented in different ways.

  4. All methods in the interface must be abstract methods. only abstract methods can be declared (and abstract modification is not required)

  5. The member attribute in the interface. only constants can be declared and variables cannot be declared.

  6. The access permissions of all members in the interface must be public, and the minimum permission in the abstract class is protected.

  7. Use a class to implement the interface. instead of using the extends keyword, use the word implements.

    • If the subclass is to override the abstract methods in the parent class interface, use implements: class-interface, abstract class-interface implements. Interface-interface extends

Implements

1. multiple interfaces can be implemented, while extends can inherit only one parent class.

2. both extends and implements can be used at the same time.

Note:
  • You can use abstract classes to implement some methods in interfaces.
  • If you want subclass to create objects, you must implement all the abstract methods in the interface.

  • You can define one interface to inherit another interface.

  • A class can implement multiple interfaces (develop subclasses according to multiple specifications) and separate multiple interface names with commas (,).

  • A class can implement one or more interfaces while inheriting the parent class (inherited first and then implemented)

Polymorphism

Polymorphism is one of the three main characteristics of object-oriented design and an important feature of object-oriented design. It shows the function of dynamic binding, also known as Polymorphism ). Multi-State functions enable full extension during software development and maintenance ). In fact, the most direct definition of polymorphism is to allow different class objects with inheritance relationships to call member functions with the same name to produce different responses.

Here is a Demo about using polymorphism:

    
 
  
Head>
  ";} Function work () {echo" U disk work
  
";} Function unMount () {echo" the USB flash disk is uninstalled successfully.
";}} Class Wire implements USB // data cable enables USB function {function mount () {echo" data cable mounting
";} Function work () {echo" start data transmission
";} Function unMount () {echo" data line unmounted successfully
";}} Class PC // declare a PC class {function USB ($ usb) {$ usb-> mount (); $ usb-> work (); $ usb-> unMount () ;}} class Person // declare a Person class {function usePC () {$ pc = new PC; $ upan = new UPan; $ wire = new Wire; $ pc-> USB ($ upan); $ pc-> USB ($ wire); }}$ p1 = new Person; // instantiate a Person class $ p1-> usePC ();

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.