PHP abstract class and interface differences

Source: Internet
Author: User

The difference between abstract classes and interfaces in PHP

1) Concept

Three concepts of object-oriented: encapsulation, inheritance, polymorphism

The encapsulation of attributes and methods is the class.
The properties and methods of a class are inherited by another class, and any class in PHP can be inherited, and inherited properties and methods can be redefined by the quilt class, which is polymorphism.

Abstract classes and Abstract methods:
The so-called abstract method is only the empty "function" of the name, and the abstract method must be an empty method.
Abstract classes are classes that contain abstract methods, and abstract classes can also contain common methods.

Because an abstract method must be an empty method, any subclass that inherits the abstract class must redefine the concrete connotation of the abstract method in order to instantiate it.

Interface:
A special abstract class that contains only abstract methods.
In other words, ordinary methods and properties are not allowed in the interface class.

2) Case Study

1.php Interface Class: interface

In fact, their role is very simple, when a lot of people to develop a project, it may be to call someone else to write some of the classes, then you will ask, how do I know how the implementation of one of his functions is named, this time the PHP interface class will play a role, when we define an interface class, The way inside it is that the following subclass must be implemented, such as:

Copy the Code code as follows:


Interface Shop
{
Public function Buy ($gid);
Public function sell ($GID);
Public Function view ($GID);
}


I declare a shop interface class, defines three methods: Buy, Sell (Sell), see (view), then inherit all subclasses of this class must implement these 3 methods less than one, if the subclass does not implement these words, it will not run. In fact, the interface class is plainly, is a class template, a class of provisions, if you belong to this category, you have to follow my rules, less one can not, but specific how you do, I don't care, it is your business, such as:

Copy the Code code as follows:


Class Baseshop implements Shop
{
Public function Buy ($gid)
{
Echo (' You have purchased ID: '. $gid. ' Goods ');
}
Public function Sell ($gid)
{
Echo (' You sold the ID for: '. $gid. ' Goods ');
}
Public Function view ($gid)
{
Echo (' You've looked at the ID: '. $gid. ' Goods ');
}
}


You think, in a multi-person cooperation of large projects, with the interface class is how convenient, so you do not have to ask others, your method name of the function is what, of course, if you like this I have no way.
Conclusion: The interface class is the leader of a class, indicating the direction in which the subclass must complete its specified method.
2.php Abstract class: Abstraction
In fact, the abstract class and interface classes are very similar, remember where to see such a sentence, the abstract class like the part of the extraction, this looks very funny, in fact, it said the abstract class of truth, the role of abstract classes, when you find many of your classes in a lot of ways you constantly in the repeated writing, Then you can consider using the abstract class, you might say "I can not rewrite a class each public class I instantiate a public class, call the same method can be," Here is OK, actually the abstract class does the work of this, but he omitted you instantiate this step, It's as handy as calling this method directly, and you can overload the method. Such as:

Copy the Code code as follows:


Abstract class Baseshop
{
Public function Buy ($gid)
{
Echo (' You have purchased ID: '. $gid. ' Goods ');
}
Public function Sell ($gid)
{
Echo (' You sold the ID for: '. $gid. ' Goods ');
}
Public Function view ($gid)
{
Echo (' You've looked at the ID: '. $gid. ' Goods ');
}
}
Class Ballshop extends Baseshop
{
var $itme _id = null;
Public Function __construct ()
{
$this->itme_id = 2314;
}
Public Function open ()
{
$this->sell ($this->itme_id);
}
}


Here is an example, like the above I define a store class, pull out all of its like, buy, sell (Sell), look (view), and the abstract class has implemented these methods, then inherit its subclasses automatically obtained these methods, subclasses do its own unique things, Introduce the duplication of code and improve reusability.
Conclusion: Abstract class is a kind of service provider, has a lot of services, you do not have to use, when necessary when you use it can, if you feel not to provide service dissatisfaction, you can also do it yourself.

3) different points of their:
1. Abstract classes can have non-abstract methods and interfaces can only have abstract methods!
2. A class can inherit multiple interfaces and a class can inherit only one abstract class!
3. The use of the interface through the Implements keyword, abstract class is inherited by the extends keyword!

4. 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)

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

6, 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.

PHP abstract class and interface differences

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.