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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles