Home > Backend Development > PHP Tutorial > What is the php object interface? Detailed explanation of various class interface codes

What is the php object interface? Detailed explanation of various class interface codes

伊谢尔伦
Release: 2023-03-13 07:54:01
Original
1883 people have browsed it

Object interface

Using interface (interface), you can specify which methods a class must implement, but you do not need to define these The specific content of the method. An interface is defined through the interface keyword, just like defining a standard class, but all methods defined in it are empty. All methods defined in an interface must be public. This is a characteristic of interfaces.

Implementations

To implement an interface, use implements operator. The class must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.

note1: When implementing multiple interfaces, methods in the interfaces cannot have the same name.

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

Inheritance is a well-known programming feature, and PHP's object model also uses inheritance. Inheritance will affect the relationship between classes and objects, and between objects. For example, when extending a class, the subclass inherits all the public and protected methods of the parent class. Unless the subclass overrides the method of the parent class, the inherited method will retain its original functionality. Inheritance is very useful for functional design and abstraction, and adding new functions to similar objects eliminates the need to rewrite these common functions.

note3: Unless autoloading is used, a class must be defined before use. If one class extends another, the parent class must be declared before the child class. This rule applies to classes inheriting other classes and interfaces.

note:4: To implement an interface, a class must use the method that is exactly the same as the method defined in the interface. Otherwise a fatal error will result.

Constant

Constant can also be defined in the interface. Interface constants are used exactly the same as class constants, but cannot be overridden by subclasses or subinterfaces.

You can define values ​​that remain unchanged in the class as constants. There is no need to use the symbol when defining and using constants. The value of a constant must be a fixed value and cannot be a variable, class attribute, the result of a mathematical operation, or a function call. Constants can also be defined in interfaces.

interface example:

Example #1 Interface example

<?php

// 声明一个&#39;iTemplate&#39;接口
interface iTemplate
{
    public function setVariable($name, $var);
    public function getHtml($template);
}


// 实现接口
// 下面的写法是正确的
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(&#39;{&#39; . $name . &#39;}&#39;, $value, $template);
        }
 
        return $template;
    }
}

// 下面的写法是错误的,会报错,因为没有实现 getHtml():
// 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;
    }
}
?>
Copy after login

Example #2 Extensible Interface

<?php
interface a
{
    public function foo();
}

interface b extends a
{
    public function baz(Baz $baz);
}

// 正确写法
class c implements b
{
    public function foo()
    {
    }

    public function baz(Baz $baz)
    {
    }
}

// 错误写法会导致一个致命错误
class d implements b
{
    public function foo()
    {
    }

    public function baz(Foo $foo)
    {
    }
}
?>
Copy after login

#Example #3 Inherit 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()
    {
    }
}
?>
Copy after login

Example #4 Using interface constants

<?php
interface a
{
    const b = &#39;Interface constant&#39;;
}

// 输出接口常量
echo a::b;

// 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。
class b implements a
{
    const b = &#39;Class constant&#39;;
}
?>
Copy after login

The above is the detailed content of What is the php object interface? Detailed explanation of various class interface codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template