PHP abstract class and interface examples and differences

Source: Internet
Author: User
Tags abstract php class

Abstract classes and interfaces (interface) are object-oriented concepts that are very similar in that they define the methods to be implemented, but there are different usage scenarios, and what is the difference between the abstract classes and interfaces of PHP, see the following example.

Interface (Interface)--defining behavior
Abstract class--implementation behavior
Specific class--execution behavior


One. Interface

interface defines the specification of a function, declares the required functions and constants, but does not specify how to implement them. An interface is a specification of a method in a specific class.

An interface defines the method, method name that is required for a behavior to occur. The specific classes are then allowed to implement these methods.
Note: Interface classes cannot define class members. The method defined must appear in the implemented class.

Interface Interface Name
{
Function Method 1 ();
Function Method 2 ();
}
Implementing interfaces
Class name implements Interface name
{
Method 1 ();
Method 2 ();
}

Two. Abstract class

An abstract class is an instantiated class that can only be used as a base class for other classes of grass. For example, you can write an abstract class about read, and then read and paper read to inherit the read abstract class.
Abstract class class name
{
Various methods
}

Abstract classes can ensure consistency because any derived class must implement all abstract methods from the abstract class, and if not, an error occurs.

Should I use an abstract class? Or an interface?
This is a problem

* * If you want to build a class model, this model will be adopted by some closely related objects, using abstract classes. If the model will be adopted by a less relevant object, use an interface.
* * If a specific class will inherit behavior from multiple sources, use an interface. A PHP class can inherit multiple interfaces, but it cannot inherit multiple abstract classes.
* * If all classes share a common behavior implementation, use an abstract class and implement that behavior in it ... The subclass inherits an abstract class, and the inherited subclass can implement the original behavior.

Pick: _____

The appearance of things, there is always a reason for its emergence (demand)!
However, the survival of things, need to be adaptable (flexible)!
For example: Giraffe, neck Long, this is an abstract concept;
Can eat grass (the characteristics of non-carnivorous animals, interface).

So, when we need to describe a giraffe, we can define it with an interface (a non carnivorous animal) and an abstract (neck-length) method.
Then, specifically what kind/or color of the giraffe, how to eat grass method, this is the specific class

PHP Interface Class

First look at the interface, set an interface class, interface class:


<?php

namespace Frontend\models;

Interface TestInterface
{
Public function cloud ();

Public function Sky ();

public static function color ($color);
}

In PHP, an interface can only define methods (or static methods), but it cannot implement specific methods. In Phpstorm, if you write a specific method, you will be prompted with an error. interface more stringent definition of what needs to be implemented, only write to implement the method on the line, do not need to implement, or define member variables.

To use an interface:

<?php

namespace Frontend\models;

Class Test implements TestInterface
{
Public Function Cloud ()
{
Echo ' Cloud ... ';
}

Public Function Sky ()
{

}

public static function Color ($color) {

}
}
To inherit an interface class, you must implement each method defined by the interface, or you will also get an error. This is the interface of PHP, which defines exactly which methods are implemented and must be implemented in subclasses.

PHP abstract class

We define an abstract class:


Abstract class Testabstract
{
Public $abstract = ' abstract ';
abstract function hehe ();

Public Function Hehe2 ()
{
Echo ' Hehe2 ';
}
}

Here are two methods, one of which is decorated with abstract, which means that the method is a subclass that must be implemented, whereas an abstract class can only be defined as not specific. A method is an ordinary member method, and there is a concrete implementation, subclasses can also be overridden.


Class AB extends Testabstract
{
Public Function hehe ()
{
echo ' hehe ';
}
}

Subclasses inherit abstract classes, simply implement the abstract methods defined in the abstract class. An abstract class can define a member variable, but the interface is not.

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.