PHP design mode (2): Abstract classes and interfaces

Source: Internet
Author: User
PHP design mode (2): Abstract class and interface original address: PHP design mode (2): Abstract class and interface

Introduction

For programming, the abstraction of things is a common topic. abstract problems are more conducive to object-oriented programming and programming patterns. Like C/C ++, Java, Python, and other languages, PHP also supports object-oriented, but slightly different. for example, PHP supports defining constants in interfaces, abstract variables are not supported.

Abstract/define action

Abstraction of Things refers to distinguishing the essential features between two different things. These two things should have a clear distinction between boundaries from a certain perspective.

For example, whale and carp, these two things have clearly defined boundaries in the perspective of animals and belong to different animals; but in the perspective of aquatic animals, they belong to the abstraction of the same animal.

Reasonably abstract the problem and construct a model. it will be easier to solve the problem through programming.

Remember: Abstraction is the basis for programming to solve problems. The more complicated the problem is, the more necessary it is to abstract the problem from the very beginning, rather than writing code directly.

Abstract Class/Abstract Class

Abstract class is a programming concept. it is called Abstract Classes in PHP. In the design mode, abstract classes cannot be instantiated or initialized, but can be implemented by inheritance of specific classes. A little abstract, right? Use code to explain:

 

The abstract class "animal" is defined. the attribute of an animal is the name, and then there is a way to eat food.

Why are animals abstract classes? Because the species of animals are not something that exists in the natural world, they are abstract in human minds. There are deterministic animals like whales and carp in nature.

For example, the concept of a whale should belong to an Animal and inherit the Animal class. we define the whale class and the way to eat:

 name = "Whale";  }  public function eat($food) {    echo $this->name . " eat " . $food . ".\n";  }}?>

Now we can initialize the whale class and call the method to eat it:

 eat("fish");?>

Run:

$ php Whale.phpWhale eat fish.
Interface/Interface

PHP also supports interfaces in the process-oriented programming concept. The following is an example of a whale:

 

Define a whale class to implement the above interface:

 

Now we can initialize the whale class and call the method to eat it:

 eat("fish");?>

Run:

$ php Whale.phpWhale eat fish.
Abstract Class vs interface

The example of the abstract class and interface above looks similar? In fact, for PHP programming, abstract classes can implement functions and interfaces.

The difference between an abstract class interface lies not in programming, but in different programming modes.

In general, abstraction is used for different things, and interfaces are used for things.

For example, an aquatic creature is an abstract concept of a whale, but an aquatic creature is not a whale's behavior. eating is a whale's behavior.

For large projects, objects are inherited and implemented by basic abstract classes, and the methods of these classes are usually defined by interfaces.

In addition, we recommend that you use an interface to change the attributes of a transaction, instead of directly assigning values or using other methods, such:

 observeEat($whale);    $this->observeEat($carp);  }  function observeEat(IAction $animal) {    $animal->eat();  }}$observer = new observer();?>

Run:

$ php Observer.phpWhale eat fish.Carp eat moss.
Summary

A good design pattern strictly abstracts the problem. although abstract classes and interfaces are similar to programming implementations, the program design pattern is different.

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.