PHP interface and Abstract Learning example

Source: Internet
Author: User
Tags abstract

Every time we learn the interface and abstraction, I will make a mistake, and I don't know what the difference between the two is. Today to move the book to organize the idea to take out, I hope to learn new PHP friends help.

1.php Interface Class: interface

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

The code is as follows:

Interface Shop

{

Public function ($gid);

Public function sell ($GID);

Public Function view ($GID);

}

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

The code is as follows:

Class Baseshop implements Shop

{

Public function Buy ($gid)

{

Echo (' you purchased the product with ID: '. $gid. ');

}

Public function Sell ($gid)

{

Echo (' You sold the product with ID: '. $gid. ');

}

Public Function view ($gid)

{

Echo (' You look at the product with ID: '. $gid. ');

}

}

You think, in a large number of people cooperation project inside, have the interface class is how convenient, so you do not have to ask others, your certain function of the method name is, 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: Abstract

In fact, abstract classes and interface classes are a lot like, the role of abstract classes, when you find that many of your classes in many ways you continue to write repeatedly, then you can consider using abstract classes, and you might say, "I'm not allowed to rewrite a class for every public class I instantiate one of these common classes, call the same method," Here is what can, in fact, be done by an abstract class, but he saves you from instantiating this step, making you as handy as calling this class method directly, and you can overload the method. Such as:

The code is as follows:

Abstract class Baseshop

{

Public function Buy ($gid)

{

Echo (' you purchased the product with ID: '. $gid. ');

}

Public function Sell ($gid)

{

Echo (' You sold the product with ID: '. $gid. ');

}

Public Function view ($gid)

{

Echo (' You look at the product with ID: '. $gid. ');

}

}

Class Ballshop extends Baseshop

{

var $itme _id = null;

Public Function __construct ()

{

$this->itme_id = 2314;

}

Public Function open ()

{

$this->sell ($this->itme_id);

}

}

Here's an example, like the above I've defined a store class, pulling out all the parts of it, buying (Buy), selling (Sell), looking (view), and implementing these methods in an abstract class, the subclass of inheriting it automatically obtains these methods, and subclasses do their own unique things, Introduce the duplication of code and improve the reusability.

Finally summed up: The abstract class is a class of service providers, with a large number of services, you do not have to use, when needed when you can use, if you do not feel satisfied with the service, you can do it yourself.

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.