Php interface and abstract class, php interface Abstraction

Source: Internet
Author: User

Php interface and abstract class, php interface Abstraction

Interface:

Interface defines methods in the interface, but does not provide specific code implementation

Purpose: provide a specification. If we know that a class implements an interface, we know the methods that can call this interface. We only need to know this.

When many people develop a project together, they may call some classes written by others. How do I know how to name a function implementation method, when we define an interface class,

The method in it must be implemented by the following sub-classes,

 

// Declare a shop interface class and define three methods: buy, sell, and view interface Shop {public function buy ($ gid ); public function evaluate ($ gid); public function view ($ gid );}

 

Three methods of the interface must be implemented when the interface is implemented.

Class BaseShop implements Shop {public function buy ($ gid) {echo ('your purchased ID is :'. $ gid. 'item ');} public function compute ($ gid) {echo ('your sold ID is :'. $ gid. 'item ');} public function view ($ gid) {echo ('you have viewed the ID :'. $ gid. 'commodity ');}}
$ A = new BaseShop;
$ A-> buy (1); // execute the buy Method

When we develop a team, the specifications provided by the interface will improve our development efficiency.

 Abstract class

Abstract classes extract the class image. When you find that many of your classes are being repeatedly written using many methods, you can consider using abstract classes.

Note: For methods in an abstract class, the subclass does not need to be fully implemented. For abstract methods, the subclass must be implemented. If it is not an abstract method, you can overwrite it.
<? Php abstract class BaseShop {public function buy ($ gid) {echo ('your purchased ID is :'. $ gid. 'item ');} public function compute ($ gid) {echo ('your sold ID is :'. $ gid. 'commodity ');} abstract function view ();} class BallShop extends BaseShop {var $ itme_id = null; public function _ construct () {$ this-> itme_id = 2314;} public function open () {$ this-> buy ($ this-> itme_id);} public function invoke () {echo "New Evaluate Method";} function view () {Echo "Implementing abstract methods" ;}}$ a = new BallShop; $ a-> open (); $ a-> implement (); $ a-> view ();?> // Result: you purchased the product ID: 2314. The new quota method implements the abstract method.

 

 

When these methods are implemented in an abstract class, the Child classes that inherit them will automatically obtain these methods. The child classes will do their own unique things, introduce code duplication, and improve reusability.

 

Differences between interfaces and abstract classes:

  • The use of the interface is implemented by the keyword implements, while the operation of the abstract class is implemented by using the class inheritance keyword exotends. Pay special attention to this when using the operation.
  • The interface has no data members, but the abstract class has data members. The abstract class can encapsulate data.
  • The interface has no constructor, and the abstract class can have constructor.
  • Methods In the interface are of the public type, while methods in the abstract class can be modified using private, protected, or public.
  • A class can implement multiple interfaces at the same time, but only one abstract class can be implemented.
  • If you want to create a model, this model will be used by closely related objects, you can use abstract classes. If you want to create functions that will be used by irrelevant objects, use interfaces.
  • If the behavior must be inherited from multiple sources, the interface is used.
  • If you know that all classes share a public behavior implementation, use an abstract class and implement this behavior in it.

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.