PHP application for medium interface such as Object-oriented development (interface, implements) _php tutorial

Source: Internet
Author: User
The application key of the interface in PHP class is interface, implements, interface is a kind of special abstract class that member property is all abstract or constant, below we see several examples.

Application of interfaces 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 added to abstract.
3. The interface abstract method property is public.
4. The member property must be a constant.

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-inheriting keyword extends, where inheritance can only be singular, and interfaces may use keywords to implements multiple references and separate them with commas

1. Format: normal 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 Post interface first, and then inherit the multiple interfaces.

4. Format: interface and interface inheritance

Interface Demo3 extends Demo {
...
}

Instance:

The code is as follows Copy Code

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 "++++++++++
";
}
function fun2 () {
echo "----------
";
}
function Fun3 () {
echo "1111111111
";
}
function Fun4 () {
echo "2222222222
";
}
}

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

The interface is defined using the keyword interface, and the method in the interface is implemented using the keyword implements, and must be fully implemented.

Example:

The code is as follows Copy Code

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, at which point it is not known what users
function run (User $VC) {
$this->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
?>

To run the example, output:

VIP User Commodity price: 80 yuan

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

This example is limited to demonstrating the usage of the PHP interface and does not involve its science or not.

Implementing multiple interfaces

PHP can also implement multiple interfaces at the same time as 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 and can also be seen as a specification of a model. Interfaces are roughly different from abstract classes:

1. A subclass if you implements an interface, you must implement all the methods in the interface (whether or not it is required), or if you are inheriting an abstract class, you only need to implement the required method.
2. If the method name defined in one of the interfaces changes, then all subclasses implementing this interface need to update the method name synchronously, whereas in an abstract class if the method name changes, the corresponding method names of its subclasses are not affected, but only become a new method (relatively old method implementation).
3. Abstract classes can only be inherited, and when a subclass needs to implement functionality that needs to inherit from more than one parent class, the interface must be used.

http://www.bkjia.com/PHPjc/632642.html www.bkjia.com true http://www.bkjia.com/PHPjc/632642.html techarticle The application key of the interface in PHP class is interface, implements, interface is a kind of special abstract class that member property is all abstract or constant, below we see several examples. The interfaces in the class should ...

  • 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.