Differences and examples of abstract classes and interfaces in PHP

Source: Internet
Author: User
Tags inheritance

Abstract class and interface differences in PHP:

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 with extends. Abstract keyword to define an abstraction class or an abstract method. Abstract classes cannot be instantiated.
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). You can have member properties and member methods in an abstract class, whereas interfaces can have only methods and constants that are not implemented.
3, the interface does not have constructors, abstract classes can have constructors. (Abstract classes can have non-abstract methods, while interfaces can only have abstract methods!) )
4. Abstract classes allow only single inheritance, while interfaces can inherit multiple
5. In the wording
One, the abstract method in the interface without and cannot add the abstract keyword, the default implicit is an abstract method, and can not add the final keyword to prevent the inheritance of abstract methods. Abstract methods must be preceded by an abstract representation
Display declarations as abstract methods.
The abstract method in the interface is public by default and can only be public, and cannot be decorated with private, protected modifiers. Abstract classes can.
Abstract class or 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 is used by 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, you can use an abstract class and implement that behavior in it.

Explain:

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 () {
      & nbsp 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;
Definition of abstract class:
Abstract class ku{//Defining an abstraction
Abstract function kx ();
......
}
function AA extends ku{
Ways to implement abstract classes

function kx () {
Echo
' SDSF ';
}
}
How to use
$aa =new AA;
$aa->kx ();
1. Define methods, subclasses must fully implement all the methods in this abstraction
2. You cannot create an object from an abstract class, it is meant to be extended
3. Abstract classes usually have an abstract method without curly braces
4. Abstract methods do not need to implement specific functions, subclass to complete
5. When a subclass implements a method of an abstract class, the visibility of its subclasses must be greater than or equal to the definition of the abstract method
6. Abstract class methods can have parameters, or can be empty
7. If the abstract method has parameters, the implementation of the subclass must also have the same number of parameters
Definition of interface class:
Interface shop{
Public function ($gid);
Public function sell ($GID);
Abstract function view ($gid);
}
If you want to use an interface, you must define a few of the methods in the interface class that are not available (except abstract).
So if in a big project regardless of how others do the following method, but he must implement all the methods in this interface can!
Example: one way to implement the above interface
Class Baseshop implements shop{
Public function Buy ($gid) {
Echo ' you purchased the ID for: '. $gid. ' Commodity ';
}
Public function Sell ($gid) {
Echo ' you buy for sale ID: '. $gid. ' Commodity ';
}
Public Function View ($gid) {
Echo ' You browsed the ID as: '. $gid. ' Commodity ';
}
}
Examples of multiple inheritance of interfaces:
Interface staff_i1{//interface 1
function SetID ();
function GetID ();
}
Interface staff_i2{//interface 2
function SetName ();
function GetName ();
}

Class staff implements staff_i1,staff_i2{
Private $id;
Private $name;
function SetID ($id) {
$this->id = $id;
}
function GetID () {
return $this->id;
}

function SetName ($name) {
$this->name = $name;
}

function GetName () {
return $this->name;
}

function Otherfunc () {//This is a method that does not exist in an interface
Echo ' Test ';
}
}

Summarize

When learning PHP object-oriented, the abstract class and interface will be confused, the role of almost why it is so easy to confuse, why not leave a go? But in fact the difference between the two is still very large, if you can use PHP's two methods, object-oriented programming will be more reasonable, clear and efficient.

The use of the interface is done through keyword implements, and the operation of the abstract class is implemented using the keyword Exotends inherited by the class, with particular attention when used.
An interface has no data members, but an abstract class has data members, and an abstract class can implement the encapsulation of the data.
An interface has no constructors, and an abstract class can have constructors.
Methods in an interface are public types, and methods in an abstract class can be decorated with private, protected, or public.
A class can implement multiple interfaces at the same time, but only an abstract class.

Same point: function body can't write anything, even two curly braces can not write!!! such as: function GetName (); that'll do.

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.