Interface Usage of PHP Object-Oriented Programming

Source: Internet
Author: User
This article mainly introduces the Interface Usage of PHP object-oriented programming. It is necessary for PHP programmers to firmly master the concept. If you need it, you can refer

This article mainly introduces the Interface Usage of PHP object-oriented programming. It is necessary for PHP programmers to firmly master the concept. If you need it, you can refer

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

Related Article

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.