php object interface

伊谢尔伦
Release: 2016-11-23 14:08:44
Original
1059 people have browsed it

Using interface, you can specify which methods a certain class must implement, but you do not need to define the specific content of these methods.

The 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 the interface must be public. This is a characteristic of the interface.

Implementations

To implement an interface, use the 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.

Note:

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

Note:

Interfaces can also be inherited, by using the extends operator.

Note:

To implement an interface, a class must use the methods defined in the interface in exactly the same way. Otherwise a fatal error will result.

Constants

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

Example

Example #1 Interface example

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('{'.$name.'}',$value,$template);
        }
        return $template;
    }
}
//下面的写法是错误的,会报错,因为没有实现getHtml()
class BadTemplate implements ITemplate
{
    private $vars = array();
    public function setVariable($name,$var)
    {
        $this->vars[$name] = $var;
    }
}
Copy after login

Example #2 Extensible interface

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

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

interface a
{
    const b = 1;
}
//输出接口常量
echo a::b;
//错误写法
class b implements a
{
    const b = 1;
}
Copy after login

Interface plus type constraints provide A good way to ensure that an object contains certain methods. See instanceof operator and type constraints.


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!