PHP abstract class, interface class difference and choose "Reprint"

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/fanteathy/article/details/7309966

Difference:

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. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. Abstract classes can declare various types of member variables to implement the encapsulation of data. (The member variables in the Java interface are declared as public static final type)

<?PHPAbstract classFather {functionmeth1 () {Echo"Meth1...<br>"; }    Abstract functionmeth2 ();  Public $var 1= "Var1";  Public Static $var 2= "Var2"; Constvar3= "Var3";}classSonextendsFather {functionmeth2 () {Echo"Meth2 of Son...<br>"; }}$s=NewSon ();Echo $s->var1. " <br> ";EchoFather::$var 2." <br> ";EchoFather::var3. " <br> ";InterfaceIfather {//Public $iVar 1= "IVar1"; The interface definition cannot contain member variables//public static $iVar 2= "IVar2"; Static variables cannot be included in the interface definition here    Constivar3= "IVar3"; functioniMeth1 ();}ClassISonImplementsIfather {functioniMeth1 () {Echo"Imeth1...<br>"; }}$is=NewISon ();EchoIfather::IVar3;?>


3, interfaces do not have constructors, abstract classes can have constructors.

4, the method in the interface is the public type by default, and the methods in the abstract class can be decorated with private,protected,public.

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

An abstract class or an interface.

    • If you are creating a model, this model will be used by some closely related objects, and you can use abstract classes. The interface is used if you want to create features that will be used by some unrelated objects.
      • If the behavior must be inherited from multiple sources, the interface is used.
      • If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it.

PHP abstract class, interface class difference and choose "Reprint"

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.