A detailed explanation of the differences between PHP abstract classes and interfaces

Source: Internet
Author: User
Tags inheritance


Difference:

1, the use of the interface is through the keyword implements. The use of abstract classes is extends by keyword. Of course, interfaces can also be inherited by keyword extends.
2. You cannot declare member variables (including class static variables) in an interface, but you can declare class constants. You can declare various types of member variables in an abstract class to implement the encapsulation of the data. (The member variables in the other Java interface are declared as public static final types)
3, the interface does not have constructors, abstract classes can have constructors.

4, the methods in the interface are public types by default, and 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 can only inherit from an abstract class.


An abstract class or an interface.

If you want to create a model that will be adopted by tightly related objects, you can use abstract classes. If you want to create a feature that will be taken by some unrelated objects, use an interface.
If you must inherit behavior from multiple sources, use an interface.
If you know that all classes share a common behavior implementation, use an abstract class and implement that behavior in it

Example

<?php
Abstract class Father {
function Meth1 () {
echo "Meth1...<br>";
}
Abstract function meth2 ();
Public $var 1= "VAR1";
public static $var 2= "VAR2";
Const var3= "VAR3";
}
Class Son extends Father {
function Meth2 () {
echo "Meth2 of son...<br>";
}
}
$s =new Son ();
Echo $s->var1. " <br> ";
echo Father:: $var 2. " <br> ";
echo Father::var3. " <br> ";


Interface Ifather {
Public $iVar 1= "IVAR1"; Member variables cannot be included in the interface definition here
public static $iVar 2= "IVar2"; Static variables cannot be included in the interface definition here
Const ivar3= "IVAR3";
function iMeth1 ();
}
Class Ison implements Ifather {
function IMeth1 () {
echo "Imeth1...<br>";
}
}
$is =new ISON ();
Echo Ifather::ivar3;
?>

We can introduce each one in more detail.

Abstract class: is based on the class, which itself is a class, just a special class, not directly to the instance, you can define methods in the class, properties. Similar to templates, the specification allows subclasses to implement detailed functionality.

Interface: Primarily based on the specification of a method, somewhat like an abstract method in an abstract class, it is more independent than an abstract method. You can allow a class to form a new class by combining multiple methods.

Abstract class is the same point as an interface:

1, are used to declare a certain thing, the canonical name, parameters, forming modules, there is no detailed implementation details.

2, all through the class to achieve the relevant details of the work

3. In syntax, abstract methods like interfaces cannot have method bodies, that is, {} symbols

4, can use inheritance, interface can inherit the interface to form a new interface, abstract classes can inherit abstract classes to form a new abstract class

Different points of the abstract class from the interface:

1, abstract classes can have attributes, common methods, abstract methods, but the interface can not have attributes, normal methods, can have constant

2, abstract classes may not have an abstract method, but the interface must have an "abstract" method

3, there are differences in grammar

4. Abstract classes are declared with the abstract keyword before the class, and class is declared as a class, the interface is declared with interface, but cannot be declared with class, because the interface is not a class.

5. Abstract methods of abstract classes must be declared with abstract, while interfaces do not need

6. Abstract classes implement detailed abstract methods in subclasses after the subclass inherits the parent class with the extends keyword. The interface is the use of implements to allow ordinary classes in the class to implement the interface of the detailed method, and the interface can implement multiple methods at a one-time, separated by commas each interface can be

Individual features:

Abstract classes may not have abstract methods, but classes with abstract methods must be abstract classes

Abstract classes, even if they are all concrete methods, can not be instantiated, as long as the new class to inherit, the instance inheritance class can

interface allows a class to implement several different methods at once

The interface itself is abstract, but note is not an abstract class, because the interface is not a class, but its methods are abstract. So, it's also abstract.

Application and combination:

The following code is based on their own thinking, not in the actual development of the application, but this kind of writing a little peculiar. Let the abstraction be combined with the interface.

The combination of abstract class and interface

<?php
/*
Writing this program stems from your own guesses, and wants to implement an interface in an abstract class.
*/
Interface work{
Public function say ();
}
Abstract Class A implements work{
Public Function Showlove () {
echo ' Love you<br/> ';
}
}
Class B extends a{
Public function say () {
Echo ' Hello, i m in B ';
}
}
$k =new B ();
$k->say ();
/*
The above program can be implemented normally
Generic class implements interface, it becomes an abstract class, which is like adding an abstract method directly to the abstract class.
*/

The combination of interface and inheritance

A parent class is a generic class that inherits the subclass and implements the interface in subclasses.

Question: Does this approach make sense and is there such an application in actual development?

<?php
Interface kk{
Public function say ();
}
Class A {
Public function Show () {
Echo ' I am the parent class <br/> ';
}
}
Class B extends a implements kk{
Public function say () {
Echo ' I am the <br/> ' that inherits a class and implements the say interface at the same time;
}

}
$b =new B ();
$b->show ();//I am the parent class
$b->say ()//I am inheriting the class A, while implementing the Say interface

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.