PHP abstract classes and interface examples and differences-PHP source code

Source: Internet
Author: User
What are the meanings of PHP abstract classes and interfaces? What are the differences between them? Here, I will introduce you to the differences between PHP abstract classes and interfaces. The specific details are as follows, I hope the article will help you. What are the meanings of PHP abstract classes and interfaces? What are the differences between them? Here, I will introduce you to the differences between PHP abstract classes and interfaces. The specific details are as follows, I hope the article will help you.

Script ec (2); script

Abstract class and interface are very important object-oriented concepts. They are very similar. They all define implementation methods, but they have different application scenarios, what is the difference between an abstract class and an interface in PHP? refer to the following example.

Interface -- Define Behavior
Abstract Class-implement Behavior
Class: execution behavior


I. Interface

The interface defines a Function Specification, declares the required functions and constants, but cannot specify how to implement them. The interface is like a standard for methods in a specific class.

Interface defines the required method and method name for a row. Then, allow specific classes to implement these methods ..
Note: The interface class cannot define class members. The defined method must appear in the implemented class.

Interface Name
{
Function Method 1 ();
Function Method 2 ();
}
// Implementation Interface
Class name implements Interface Name
{
Method 1 ();
Method 2 ();
}

Ii. abstract class

Abstract classes are instantiated classes and can only be used as the base classes of other classes. For example, you can write an abstract class about read, and then read it electronically and read it in paper to inherit the abstract class of read.
Abstract class name
{
Methods
}

Abstract classes ensure consistency, because any Derived classes must inherit all abstract methods from this abstract class. If they are not implemented, an error is generated.

Should I use abstract classes? Or interface?
This is a problem

** To create a class model, this model is used by closely related objects and uses abstract classes. If the model is used by irrelevant objects, use interfaces.
** If a specific class inherits behavior from multiple sources, an interface is used. The php class can inherit multiple interfaces, but cannot inherit multiple abstract classes.
** If all classes share a public behavior implementation, use an abstract class and implement the behavior in it .. Use a subclass to inherit an abstract class. The inherited subclass can implement the original behavior ..

Abstract :_______

There is always a reason for a thing to appear (demand )!
However, the survival of things requires adaptability (flexibility )!
For example, a giraffe with a long neck is an abstract concept;
It can graze (features and interfaces of non-meat animals ).

So when we need to describe a giraffe, we can use interfaces (non-meat-eating animals) and abstract (neck length) Methods to define it,
Then, the specific type/color of the giraffe, how to eat grass, This is the specific class

PHP Interface Class

First, let's look at the interface and define an interface class:



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 cannot implement specific methods. If you write a specific method in PhpStorm, an error is prompted. The interface strictly defines the method to be implemented. You only need to write the method to be implemented, without specific implementation or defining member variables.

Interface:


Namespace frontend \ models;

Class Test implements TestInterface
{
Public function cloud ()
{
Echo 'cloud ....';
}

Public function sky ()
{

}

Public static function color ($ color ){

}
}
The inherited interface class must implement each method defined by the interface. Otherwise, an error is returned. This is the PHP interface, which strictly defines the implementation methods and must be implemented in the subclass.

PHP abstract class

We define an abstract class:


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

Public function hehe2 ()
{
Echo 'he2 ';
}
}

Here there are two methods. One is modified with abstract, which indicates that this method must be implemented by sub-classes, while the abstract class can only be defined and cannot be implemented. A method is a common member method, and has specific implementations. sub-classes can also be rewritten.


Class AB extends Testabstract
{
Public function hehe ()
{
Echo 'hehes ';
}
}

Subclass inherits abstract classes. You only need to implement the abstract methods defined in the abstract classes. Abstract classes can define member variables, but interfaces cannot.

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.