The interface usage _php skill of PHP object-oriented programming

Source: Internet
Author: User

Interface is a very important concept in the object-oriented programming of PHP. This paper describes the usage of PHP interface in detail in the form of an instance. Specifically as follows:

Interface: interface

In PHP, we can specify what public external operations an object should have, and use interface to specify it.
The common method is the interface. Used to specify which public action method (interface) An object should be used for, which is also called an interface (a collection of common action methods).
namely: interface (interface structure, public method collection)

Public method (interface method)
Definition: A structure that defines the public action methods that an object must have, called an Interface (interface)
Syntax: Defines the interface structure, using the interface keyword. Some common methods are defined within an interface.

Interface interface Name
{
Public action method list
}

Examples are as follows:

Interface I_goods
{public
function sayname ();
Public function Sayprice ();
}

Attention:
1. Interface method, access permission must be public
2. The interface can only have public methods, can not exist member variables
3. An interface can contain only methods that are not implemented, also called abstract methods, but do not use the abstract keyword.

The class implements the interface and completes 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 ()
{
}
}

Thus, the class that implements the interface must implement all the abstract methods within the interface. And to be sure, the method must be a public external operation.

Multi-Implementation: This function can theoretically be implemented by abstract classes, but abstract classes are not professional.
The use of interfaces is professional, implemented, because PHP supports multiple implementations, and only single inheritance is supported .

Examples are as follows:

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 ()
{
}
}

You can also inherit between interfaces
examples are as follows:

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, you can define class constants

Examples are as follows:

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::P AI;

Run output: 3.14

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.