What is the PHP object interface? A detailed description of the various class interface codes

Source: Internet
Author: User

Object interface

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods. Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty. All methods defined in the interface must be public, which is the attribute of the interface.

Implementation (implements)

To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.

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

Note2: Interfaces can also be inherited by using the extends operator.

Inheritance has been well-known for a programming feature, and the PHP object model also uses inheritance. Inheritance will affect the relationship between classes and classes, objects and objects. For example, when you extend a class, subclasses inherit all the public and protected methods of the parent class. Unless the subclass overrides the method of the parent class, the inherited method retains its original functionality. Inheritance is very useful for the design and abstraction of features, and adding new functionality to similar objects eliminates the need to re-write these common features.

Note3: Unless automatic loading is used, a class must be defined before it is used. If one class extends another, the parent class must be declared before the child class. This rule applies to classes that inherit other classes and interfaces.

Note:4: To implement an interface, a class must use exactly the same approach as the method defined in the interface. Failure to do so can result in fatal errors.

Constant

Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces.

You can define a value that is always the same in a class as a constant. You do not need to use symbols when defining and using constants. The value of a constant must be a fixed value, not a variable, a class property, a result of a mathematical operation, or a function call. You can also define constants in the interface (interface).

interface Example:

Example #1 Interface Example

<?php//declares a ' iTemplate ' interface interface itemplate{public    function SetVariable ($name, $var);    Public Function gethtml ($template);} Implement interface//the following notation is correct for 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, will be error, because there is no implementation gethtml ()://Fatal Error:class badtemplate contains 1 abstract methods//and must therefore be de clared Abstract (Itemplate::gethtml) class Badtemplate implements itemplate{    Private $vars = Array ();      Public Function SetVariable ($name, $var)    {        $this->vars[$name] = $var;    }}? >

Example #2 Expandable Interface

<?phpinterface a{public    function foo (); Interface B extends a{public    function Baz (Baz $baz);} Correct syntax Class C implements b{public    function foo ()    {    } public    function Baz (Baz $baz)    {    }}// Error writing causes a fatal error class D implements b{public    function foo ()    {    } public    function Baz (foo $foo)    {    }}? >

Example #3 inherit multiple interfaces

<?phpinterface 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

<?phpinterface a{const B = ' Interface constant ';} Output interface constant echo a::b;//error notation, because constants cannot be overwritten. The concept of an interface constant is the same as a class constant. Class B implements a{const B = ' Class constant ';}? 
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.