PHP application of interface in object-oriented development (interface, implements)

Source: Internet
Author: User
Tags abstract constant inheritance

Application of interface in class

1. Keywords: interface
2. Keywords: implements

1. Introduction and creation of the interface

Interface: A special abstract class in which member properties are all abstract or constant.

Rules:

1. All of the classes are abstract methods.
2. Abstract method Money is not to be added to abstract.
3. The interface abstract method property is public.
4. member properties must be constants.

Format:

The code is as follows Copy Code

Interface Demo {
Const NAME = "Constant object property";

function fun1 ();
function fun2 (); Abstract method.
}


2. Application and specification of interface

Interface references differ from class inheritance keyword extends, inheritance can only be singular, and interfaces can use keywords to implements multiple references and separate them with commas

1. Format: Common class Reference interface

Class MyPC implements demo, Demo2, Demo3 {
...
}

2. Format: Abstract class Application Interface example

Abstract class MyPC implements demo, Demo2, Demo3 {
...
}

3. Format: Inherit parent class reference interface coexist

Class MyPC extends Root implements demo, Demo2, Demo3 {
...
}

Inherit the latter interface first, and inherit multiple interfaces.

4. Format: interface and interface inheritance

Interface Demo3 extends Demo {
...
}

Instance:

The code is as follows Copy Code

<?php
Interface Demo {
Const NAME = "Name";
function fun1 ();
function fun2 ();
}

Interface Demo2 {
function Fun3 ();
function Fun4 ();
}

Interface Demo3 {
Const TEST = "Test";
function Fun5 ();
}

Class MyPC implements demo, Demo2 {
function Fun1 () {
echo "++++++++++<br/>";
}
function fun2 () {
echo "----------<br/>";
}
function Fun3 () {
echo "1111111111<br/>";
}
function Fun4 () {
echo "2222222222<br/>";
}
}

Class Myps extends MyPC implements DEMO3 {
function Fun5 () {
echo "Inherit class after reference interface";
}
}
$p = new Myps;
$p->fun1 ();
$p->fun2 ();
$p->fun3 ();
$p->fun4 ();
$p->fun5 ();
?>

Cases

Interfaces are defined using keyword interface and keyword implements are used to implement methods in the interface and must be fully implemented.

Example:

The code is as follows Copy Code

<?php
Defining interfaces
Interface user{
function Getdiscount ();
function Getusertype ();
}
VIP User Interface Implementation
Class Vipuser implements user{
VIP User Discount Factor
Private $discount = 0.8;
function Getdiscount () {
return $this->discount;
}
function Getusertype () {
Return "VIP user";
}
}
Class goods{
var $price = 100;
var $vc;
Defines the user interface type parameter, which does not know what users
function run (User $VC) {
$this-&GT;VC = $VC;
$discount = $this->vc->getdiscount ();
$usertype = $this->vc->getusertype ();
echo $usertype. " Commodity price: ". $this->price* $discount;
}
}

$display = new Goods ();
$display->run (new Vipuser); Can be more other user types
?>

Run the example and output:

VIP Users Commodity price: 80 yuan

This example demonstrates a simple application of a PHP interface. In this example, the user interface implements discounts for users and a specific discount factor is implemented within the Vipuser class. Finally, the commodity class goods the user interface to implement different customer quotes.

This example is limited to demonstrating the use of the PHP interface, not involving its science or not.

Implementing multiple interfaces

PHP can also implement multiple interfaces while inheriting a class:

Class Subclass extends Parent class Implemtns interface 1, interface 2, ...
{
......
}


the difference between an abstract class and an interface


An interface is a special abstract class, or it can be seen as a specification of a model. The interface differs roughly from an abstract class as follows:

1. If a subclass implements an interface, it must implement all the methods in the interface (whether or not it is needed), and if it inherits an abstract class, just implement the desired method.
2. If a method name that is defined in the interface changes, then all subclasses that implement this interface need to update the method name synchronously, and if the method name in the abstract class is changed, its subclass's corresponding method name is not affected, but becomes a new method (relatively old method implementation).
3. Abstract classes can only be inherited only, and interfaces must be used when a subclass needs to implement functionality that needs to be inherited from multiple parent classes.

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.