PHP Object-Oriented-interface

Source: Internet
Author: User

PHP Object-Oriented-interface

Interface: interface

In PHP, we can specify what public external operations an object should have, 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)

Interface (interface structure, public method collection)

Public methods (interface methods)

Defined:

A structure, called an interface (interface), used to qualify an object to have a common method of operation.

Grammar:

Define the interface structure, using the interface keyword. The interfaces are defined in a number of public methods.

Interface Interface Name

{

List of common methods of operation

}

Example:

Interface I_goods

{

Public function sayname ();

Public 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 ();

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 it is certain that this method must be a public external operation method.

Multi-Implementation

The above functions, in theory, can be implemented by abstract classes, but abstract classes, not professional.

interface specialized in, implementation, because PHP supports multiple implementations, and 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 ()

{

}

}

Can also inherit between interfaces

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

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

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.