The usage and application example of PHP interface operation interface key word

Source: Internet
Author: User
Tags abstract constant vars

An interface is a form of constraint that includes only member definitions and does not contain content that is implemented by members. with interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specific contents of those methods. We can define an interface by interface, just like defining a standard class, but all of the methods defined are empty.

All methods defined in an interface must be public, which is an attribute of the interface.

Implement

To implement an interface, you can use the implements operator. Class must implement all the methods defined in the interface, otherwise a fatal error will be reported. If you want to implement multiple interfaces, you can use commas to separate the names of multiple interfaces.

Note:

When implementing multiple interfaces, the methods in the interface cannot have duplicate names.

Note:

Interfaces can also be inherited by using the extends operator.

Constants

You can also define constants in an interface. Interface constants and class constants are used exactly the same. They are all fixed values and cannot be modified by quilts or sub-interfaces.


Example

Example #1 Interface code example

<?php

Declare a ' ITemplate ' interface
Interface ITemplate
{
Public Function SetVariable ($name, $var);
Public Function gethtml ($template);
}


Implementing interfaces
The following is the correct wording
Class Template implements ITemplate
{
Private $vars = Array ();

Public Function SetVariable ($name, $var)
{
$this->vars[$name] = $var;
}

Public Function gethtml ($template)
{
foreach ($this->vars as $name => $value) {
$template = Str_replace (' {'. $name. '} ', $value, $template);
}

return $template;
}
}

The following wording is wrong and will be an error:
Fatal Error:class Badtemplate contains 1 abstract methods
and must therefore be declared abstract (itemplate::gethtml)
Class Badtemplate implements ITemplate
{
Private $vars = Array ();

Public Function SetVariable ($name, $var)
{
$this->vars[$name] = $var;
}
}
?>

Example #2 Extendable Interfaces

<?php
Interface A
{
Public function foo ();
}

Interface B extends A
{
Public function Baz (Baz $baz);
}

Correct wording
Class C Implements B
{
Public Function foo ()
{
}

Public function Baz (Baz $baz)
{
}
}

Wrong writing can result in a fatal error
Class D implements B
{
Public Function foo ()
{
}

Public function Baz (Foo $foo)
{
}
}
?>

Example #3 inheritance between multiple interfaces

<?php
Interface A
{
Public function foo ();
}

Interface B
{
Public function bar ();
}

Interface C extends A, b
{
Public function Baz ();
}

Class D implements C
{
Public Function foo ()
{
}

Public Function Bar ()
{
}

Public Function Baz ()
{
}
}
?>

Example #4 Using Interface constants

<?php
Interface A
{
Const B = ' Interface constant ';
}

Output Interface Constants
Echo a::b;

Incorrectly, because the value of a constant cannot be modified. The concept of an interface constant is the same as a class constant.
Class B implements a
{
Const B = ' Class constant ';
}
?>

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.