Home php教程 php手册 php 接口(implement,implements)的学习和使用

php 接口(implement,implements)的学习和使用

Jun 06, 2016 pm 07:49 PM
implements php study interface

一,接口的定义和调用 1 ? php 2 interface face1 3 { 4 const param = 'test' ; 5 public function show(); 6 } 7 8 class test implements face1 9 { 10 public function show() 11 { 12 echo "interface is runbr" ; 13 } 14 } 15 16 $face = new test();

一,接口的定义和调用

<span> 1</span> <span>php
</span><span> 2</span> <span>interface</span><span> face1
</span><span> 3</span> <span>{
</span><span> 4</span>  <span>const</span> param = 'test'<span>;
</span><span> 5</span>  <span>public</span> <span>function</span><span> show();
</span><span> 6</span> <span>}
</span><span> 7</span> 
<span> 8</span> <span>class</span> test <span>implements</span><span> face1
</span><span> 9</span> <span>{
</span><span>10</span>  <span>public</span> <span>function</span><span> show()
</span><span>11</span> <span> {
</span><span>12</span>  <span>echo</span> "interface is run<br>"<span>;
</span><span>13</span> <span> }
</span><span>14</span> <span>}
</span><span>15</span> 
<span>16</span> <span>$face</span> = <span>new</span><span> test();
</span><span>17</span> <span>echo</span> <span>$face</span>->show();         <span>//</span><span>inerface is run</span>
<span>18</span> <span>echo</span> face1::param;           <span>//</span><span>test</span>
<span>19</span> ?>
Copy after login

说明:上面的例子要注意一点,接口的方法名是show,继承接口的类中必须有show这个方法,要不然就会报错。也就是说接口的方法是假的,真正起作用的是在继承的类中的方法,就是因为这一点,所以我觉得,接口根php的抽象类有点像。

二,对参数约束比较严

<span>php
</span><span>interface</span><span> face1
{
 </span><span>public</span> <span>function</span> show(show <span>$show</span><span>);
}

</span><span>//</span><span> 显示正常</span>
<span>class</span> test <span>implements</span><span> face1
{
 </span><span>public</span> <span>function</span> show(show <span>$show</span><span>)
 {
 </span><span>echo</span> "asdfasdf"<span>;
 }
}

</span><span>//</span><span> 报fatal错误</span>
<span>class</span> test2 <span>implements</span><span> face1
{
 </span><span>public</span> <span>function</span> show(aaa <span>$aaa</span><span>)
 {
 }
}
</span>?>
Copy after login

说明:上面的这个例子报fatal错误的,为什么会报fatal错误呢?原因就在所传参数是aaa $aaa,而不是show $show。继承接口类中,调用接口的方法时,所传参数要和接口中的参数名要一至。不然就会报错。

三,接口间的继承和调用接口传递参数

<span>php
</span><span>interface</span><span> face1
{
 </span><span>public</span> <span>function</span><span> show();
}

</span><span>interface</span> face2 <span>extends</span><span> face1
{
 </span><span>public</span> <span>function</span> show1(test1 <span>$test</span>,<span>$num</span><span>);
}

</span><span>class</span> test <span>implements</span><span> face2
{
 </span><span>public</span> <span>function</span><span> show()
 {
 </span><span>echo</span> "ok<br>"<span>;
 }

 </span><span>public</span> <span>function</span> show1(test1 <span>$test</span>,<span>$num</span><span>)
 {
 </span><span>var_dump</span>(<span>$test</span><span>);
 </span><span>echo</span> <span>$test1</span>->aaaa."<span>$num</span><br>"<span>;
 }
}

</span><span>class</span><span> test1
{
 </span><span>public</span> <span>$aaaa</span>="this is a test"<span>;
 </span><span>function</span><span> fun(){
 </span><span>echo</span> ' ===============<br>'<span>;
 }
}

</span><span>$show</span> = <span>new</span><span> test1;
</span><span>$show</span>->fun();            <span>//</span><span>显示===============</span>
test::show();             <span>//</span><span>显示ok</span>
test::show1(<span>$show</span>,6);     <span>//</span><span>object(test1)#1 (1) { ["aaaa"]=>  string(14) "this is a test" } 6</span>
?>
Copy after login

说明:上面的例子可以看到,接口face2继承了接口face1,类test继承了接口face2。不知道你发现没有,class类test当中包括有二个方法,一个是show,一个show1,并且一个也不能少,如果少一个,报fatal错误。show1(test1 $test,$num)中的test1必须根继承类的名子要一样classtest1。如果不一样,也会报fatal错误。那如果一个接口被多个类继承,并且类名又不一样,怎么办呢?那就要用self了,下面会提到

四,一个接口多个继承

<span>php
</span><span>interface</span><span> Comparable {
 </span><span>function</span> compare(self <span>$compare</span><span>);
}

</span><span>class</span> <span>String</span> <span>implements</span><span> Comparable {
 </span><span>private</span> <span>$string</span><span>;
 </span><span>function</span> __construct(<span>$string</span><span>) {
 </span><span>$this</span>-><span>string</span> = <span>$string</span><span>;
 }

 </span><span>function</span> compare(self <span>$compare</span><span>) {
 </span><span>if</span>(<span>$this</span>-><span>string</span> == <span>$compare</span>-><span>string</span><span>){
 </span><span>return</span> <span>$this</span>-><span>string</span>."==".<span>$compare</span>-><span>string</span>."<br>"<span>;
 }</span><span>else</span><span>{
 </span><span>return</span> <span>$this</span>-><span>string</span>."!=".<span>$compare</span>-><span>string</span>."<br>"<span>;
 }
 }
}

</span><span>class</span> <span>Integer</span> <span>implements</span><span> Comparable {
 </span><span>private</span> <span>$integer</span><span>;
 </span><span>function</span> __construct(<span>$int</span><span>) {
 </span><span>$this</span>-><span>integer</span> = <span>$int</span><span>;
 }

 </span><span>function</span> compare(self <span>$compare</span><span>) {
 </span><span>if</span>(<span>$this</span>-><span>integer</span> == <span>$compare</span>-><span>integer</span><span>){
 </span><span>return</span> <span>$this</span>-><span>integer</span>."==".<span>$compare</span>-><span>integer</span>."<br>"<span>;
 }</span><span>else</span><span>{
 </span><span>return</span> <span>$this</span>-><span>integer</span>."!=".<span>$compare</span>-><span>integer</span>."<br>"<span>;
 }
 }
}

</span><span>$first_int</span> = <span>new</span> <span>Integer</span>(3<span>);
</span><span>$second_int</span> = <span>new</span> <span>Integer</span>(4<span>);
</span><span>$first_string</span> = <span>new</span> <span>String</span>("foo"<span>);
</span><span>$second_string</span> = <span>new</span> <span>String</span>("bar"<span>);

</span><span>echo</span> <span>$first_int</span>->compare(<span>$second_int</span>);              <span>//</span><span> 3!=4</span>
<span>echo</span> <span>$first_int</span>->compare(<span>$first_int</span>);               <span>//</span><span> 3==3</span>
<span>echo</span> <span>$first_string</span>->compare(<span>$second_string</span>);        <span>//</span><span> foo!=bar</span>
<span>echo</span> <span>$first_string</span>->compare(<span>$second_int</span>);           <span>//</span><span> 严重错误</span>
?>
Copy after login

说明:从上面的例子中可以看出,一个接口可以被多个类继承,并且类名不一样。同一个类之间可以相互调用,不同类之间不能调用。echo $first_string->compare($second_int);报fatal错误的。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles