Differences and selection analysis of abstract classes and interfaces in PHP, php abstraction _ PHP Tutorial

Source: Internet
Author: User
Differences and selection analysis of abstract classes and interfaces in PHP, and php abstraction. The differences and selection analysis of abstract classes and interfaces in PHP. the example in this article analyzes the differences and selection of abstract classes and interfaces in php. For your reference, the differences between abstract classes and interfaces in PHP are as follows:

This article analyzes the differences and selection of abstract classes and interfaces in PHP. We will share this with you for your reference. The details are as follows:

Differences:

1. the use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course, the interface can also be inherited by the keyword extends.

2. member variables (including static class variables) cannot be declared in the interface, but class constants can be declared. Abstract classes can declare various types of member variables to encapsulate data. (The member variables in the JAVA interface must be declared as public static final)

<? Phpabstract class Father {function meth1 () {echo "meth1...
";}Abstract function meth2 (); public $ var1 =" var1 "; public static $ var2 =" var2 "; const Var3 =" Var3 ";} class Son extends Father {function meth2 () {echo "meth2 of Son...
";}}$ S = new Son (); echo $ s-> var1 ."
"; Echo Father: $ var2 ."
"; Echo Father: Var3 ."
"; Interface IFather {// public $ iVar1 =" iVar1 "; the Interface definition cannot contain the member variable // public static $ iVar2 =" iVar2 "; the interface definition cannot contain the static variable const iVar3 = "iVar3"; function iMeth1 ();} Class ISon implements IFather {function iMeth1 () {echo "imeth1...
";}}$ Is = new ISon (); echo IFather: iVar3;?>

3. the interface has no constructor, and the abstract class can have constructor.

4. methods in the interface are of the public type by default, while methods in the abstract class can be modified using private, protected, and public.

5. a class can implement multiple interfaces at the same time, but a class can only inherit from one abstract class.

Abstract class or interface

1. if you want to create a model, this model will be used by closely related objects, you can use abstract classes. If you want to create functions that will be used by irrelevant objects, use interfaces.

2. if the behavior must be inherited from multiple sources, use the interface.

3. if you know that all classes share a public behavior implementation, use the abstract class and implement this behavior in it.

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.