PHP Object-Oriented Programming interface usage, PHP object-oriented programming
Interface is a very important concept in the object-oriented programming of PHP. In this paper, the usage of PHP interface is described in detail in the instance form. Specific as follows:
Interface: interface
In PHP, we can specify what public external operations an object should have, which can be specified using interface.
The common method is the interface. Used to specify which public operation methods (interfaces) An object should be used for, which is also called an interface (a collection of public action methods)
namely: interface (interface structure, public method collection)
Public methods (interface methods)
Definition: A structure used to qualify an object that must have a common operating method, called an interface (interface)
Syntax: Defines the interface structure, using the interface keyword. The interfaces are defined in a number of public methods.
Interface interface Name {public action method list}
Examples are as follows:
Interface I_goods{public function Sayname ();p ublic function Sayprice ();
Attention:
1. Interface methods, access permissions must be public
2. There can be only public methods within the interface, no member variables exist
3. Interfaces can only contain methods that are not implemented, also called abstract methods, but do not use the abstract keyword.
The implements class implements the interface and completes with the keyword.
Example:
Interface I_goods{public function Sayname ();p ublic function Sayprice (); Class Goods implements I_goods{public function Sayname () {}public function Sayprice () {}}
Thus, the class that implements the interface must implement all the abstract methods within the interface. And it is certain that this method must be a public external operation method.
Multi-Implementation: This function, in theory, can be achieved by abstract classes, but abstract classes, not professional.
The use of interfaces is professional, implementation, because PHP supports a multi-implementation, and only support single inheritance .
Examples are as follows:
Interface I_goods{public function Sayname ();p ublic function Sayprice (); Interface I_shop{public function Saysafe ();} Class Goods implements I_goods, I_shop{public function Sayname () {}public function Sayprice () {}public function Saysafe () { }}
Can also inherit between interfaces
Examples are as follows:
Interface I_goods{public function Sayname ();p ublic 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, you can define class constants
Examples are as follows:
Interface I_goods{const PAI = 3.14;public function sayname ();p ublic 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::P ai;
Run output: 3.14
For PHP and other object-oriented programming interface problems, the answer
PHP interface is not with other programming languages with DE, but for the future development of a template, you follow this template to do it.
For example, about database operations. Interface can be written like this, set insert,update,select,delete these four methods, the database is nothing more than delete and change to check four operations.
Other programmers as long as the implementation (Inplements) This interface, according to these four methods of thinking to write the program on the line, write well on the implementation of the database interface.
So for the Software master Designer, as long as the software to do a few things (interface), the following programmers follow these few things to do. Interface is not for multi-person development of the system, there are advantages?
As for the common API interface, actually say simple, is the method function class, others write good method, we just go to call can.
Why does PHP object use interfaces, abstract classes, and what roles do they play? Just a little bit more about it.
Abstract class:
is used to inherit, itself can not be instantiated, is used to set norms, subclasses must be the parent class of the abstract methods to implement all
Interface:
Can be understood as a more rigorous abstract class
One is like an abstract class, you can set the specification, because the interface has a feature, the implementation of the interface must be the method of all implementation, so that the project manager can set a specification in the interface, to implement which functions
Second, PHP is a single inheritance, a class can only have a parent class, in order to solve the problem there is an interface, a class can implement multiple interfaces
http://www.bkjia.com/PHPjc/866658.html www.bkjia.com true http://www.bkjia.com/PHPjc/866658.html techarticle PHP Object-oriented Programming interface usage, PHP object-oriented programming interface is a very important concept of PHP object-oriented programming. This article is described in detail in the form of examples ...