Interface Usage of PHP object-oriented programming, php Object-Oriented Programming

Source: Internet
Author: User

Interface Usage of PHP object-oriented programming, php Object-Oriented Programming

Interfaces are an important concept in PHP object-oriented programming. This article describes the usage of PHP interfaces in details in the form of instances. The details are as follows:

Interface: interface

In PHP, we can specify the public external operations that an object should have, and use interfaces to specify.
The common method is the interface. Specifies which public operation methods (interfaces) an object should be used for. This is also called an interface (a set of public operation methods)
Interface (interface structure, public method set)

Public method (interface method)
Definition: A structure used to limit the public operation methods required by an object. It is called an interface)
Syntax: defines the interface structure and uses the interface keyword. Some common methods are defined in the interface.

Interface name {list of public operation methods}

Example:

interface I_Goods{public function sayName();public function sayPrice();}

Note:
1. interface method. The access permission must be public.
2. There can only be public methods in the interface, and member variables cannot exist.
3. The interface can only contain unimplemented methods, also called abstract methods, but does not need abstract keywords.

Class implementation interface, using the keyword implements.

Example:

interface I_Goods{public function sayName();public function sayPrice();}class Goods implements I_Goods{public function sayName(){}public function sayPrice(){}}

In this way, the class that implements this interface must implement all the abstract methods in the interface. It is also certain that this method must be a public external operation method.

Multiple implementations: this function can be theoretically implemented through abstract classes, but the abstract classes are not professional.
The use of interfaces is more professional, because php supports multiple implementations, but only supports single inheritance.

Example:

interface I_Goods{public function sayName();public function sayPrice();}interface I_Shop{public function saySafe();}class Goods implements I_Goods , I_Shop{public function sayName(){}public function sayPrice(){}public function saySafe(){}}

Interfaces can also be inherited.
Example:

interface I_Goods{public function sayName();public function sayPrice();}interface I_Shop extends I_Goods{public function saySafe();}class Goods implements I_Shop{public function sayName(){}public function sayPrice(){}public function saySafe(){}}

Php Object Interface Support, class constants can be defined

Example:

interface I_Goods{const PAI = 3.14;public function sayName();public function sayPrice();}interface I_Shop extends I_Goods{public function saySafe();}class Goods implements I_Shop{public function sayName(){}public function sayPrice(){}public function saySafe(){}}echo Goods::PAI;

Output: 3.14


Answers to questions about interfaces between php and other Object-Oriented Programming

PHP interfaces are not used in combination with other programming languages, but are used to create a template for future development. You can simply follow this template.
For example, database operations. Interface can be written in this way, set the insert, update, select, delete methods, the database is nothing more than add, delete, modify, query four operations.
Other programmers only need to implement the (inplements) interface and write the program according to the four methods. After writing the interface, the database interface is implemented.

In this way, for the software chief designer, as long as the software is given to do a few things (interfaces), the following programmers can do well according to these things. Is the interface very advantageous in the multi-person development system?

As for common API interfaces, a simple one is the method function class. For Methods written by others, we only need to call them.

Why are php object-oriented interfaces and abstract classes used? What roles do they play? Let's take a closer look.

Abstract class:
It is used for inheritance and cannot be instantiated. It is used for Standardization. Subclass must implement all abstract methods of the parent class.
Interface:
It can be understood as a stricter abstract class.
First, like an abstract class, you can set standards. Because an interface has its own characteristics, you must implement all the methods in the interface, so that the project manager can define a specification in the interface, functions to be implemented
Second, php is a single inheritance. A class can only have one parent class. To solve this problem, an interface occurs. A class can implement multiple interfaces.

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.