用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] * * * * * ##1 接口意义 1 接口用来**约定类的功能**。指定类必须实现的功能方法。 2 接口文件中使用**interface**关键字声明接口,在其中**列出需要实现的功能方法名称**。 3 接口中的方法的**修饰符必须是public**,并且**方法体为空**。 4 接口也可以使用**extends扩展接口**,也可**定义常量**。 ##2 接口使用 ### 2-1 接口简单使用示例 ~~~ <?php // 声明'iTemplate'接口,列出需要实现的功能方法名称 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 ; } } ~~~ ### 2-2 使用extends扩展接口 ~~~ // 使用接口b扩展接口a interface a { public function foo (); } interface b extends a { public function baz ( Baz $baz ); } //创建类实现扩展后的接口b class c implements b { public function foo () { } public function baz ( Baz $baz ) { } } ~~~ ~~~ //创建两个并列接口 interface a { public function foo (); } interface b { public function bar (); } //接口c同时继承a与b接口 interface c extends a , b { public function baz (); } //创建类d实现接口c class d implements c { public function foo () { } public function bar () { } public function baz () { } } ~~~ ###2-3 在接口中使用常量 ~~~ <?php //接口a中包含常量b interface a { const b = 'Interface constant' ; } //输出接口常量 echo a :: b ; ~~~ ## 3 常用接口 >[info] php中预定义了特定功能接口。 ### 3-1 迭代器接口Iterator > 1 接口功能方法 ~~~ Iterator extends Traversable { //返回当前键的值 abstract public mixed current ( void ) //返回当前键名 abstract public scalar key ( void ) //移动到下一个 abstract public void next ( void ) //返回开头 abstract public void rewind ( void ) //检查当前键名是否有效 abstract public boolean valid ( void ) } ~~~ > 2 接口实例 ~~~ <?php ;自定义类实现迭代器接口功能方法 class myIterator implements Iterator { private $position = 0 ; private $array = array( "firstelement" , "secondelement" , "lastelement" , ); public function __construct () { $this -> position = 0 ; } function rewind () { var_dump ( __METHOD__ ); $this -> position = 0 ; } function current () { var_dump ( __METHOD__ ); return $this -> array [ $this -> position ]; } function key () { var_dump ( __METHOD__ ); return $this -> position ; } function next () { var_dump ( __METHOD__ ); ++ $this -> position ; } function valid () { var_dump ( __METHOD__ ); return isset( $this -> array [ $this -> position ]); } } ;创建迭代器对象 $it = new myIterator ; ;使用foreach遍历迭代器对象 foreach( $it as $key => $value ) { var_dump ( $key , $value ); echo "\n" ; } ?> ~~~ 输出 ~~~ string(18) "myIterator::rewind" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(0) string(12) "firstelement"string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(1) string(13) "secondelement"string(16) "myIterator::next" string(17) "myIterator::valid" string(19) "myIterator::current" string(15) "myIterator::key" int(2) string(11) "lastelement"string(16) "myIterator::next" string(17) "myIterator::valid" ~~~ **PHP内置了SPL迭代器** ### 3-2 数组访问接口ArrayAccess > 1 接口功能方法 ~~~ ArrayAccess { ;检查数组下标是否有效 abstract public boolean offsetExists ( mixed $offset ) ;获取数组下标的对应值 abstract public mixed offsetGet ( mixed $offset ) ;修改数组下标的对应值 abstract public void offsetSet ( mixed $offset , mixed $value ) ;删除数组下标的值 abstract public void offsetUnset ( mixed $offset ) } ~~~ > 2 接口使用 ~~~ ;创建类实现数组访问接口 class obj implements Arrayaccess { private $container = array(); public function __construct () { $this -> container = array( "one" => 1 , "two" => 2 , "three" => 3 , ); } public function offsetSet ( $offset , $value ) { if ( is_null ( $offset )) { $this -> container [] = $value ; } else { $this -> container [ $offset ] = $value ; } } public function offsetExists ( $offset ) { return isset( $this -> container [ $offset ]); } public function offsetUnset ( $offset ) { unset( $this -> container [ $offset ]); } public function offsetGet ( $offset ) { return isset( $this -> container [ $offset ]) ? $this -> container [ $offset ] : null ; } } ;创建数组访问对象 $obj = new obj ; ;数组访问操作 var_dump (isset( $obj [ "two" ])); var_dump ( $obj [ "two" ]); unset( $obj [ "two" ]); var_dump (isset( $obj [ "two" ])); $obj [ "two" ] = "A value" ; var_dump ( $obj [ "two" ]); $obj [] = 'Append 1' ; $obj [] = 'Append 2' ; $obj [] = 'Append 3' ; print_r ( $obj ); ~~~ 输出 ~~~ bool(true) int(2) bool(false) string(7) "A value" obj Object ( [container:obj:private] => Array ( [one] => 1 [three] => 3 [two] => A value [0] => Append 1 [1] => Append 2 [2] => Append 3 )) ~~~ ### 3-3 Json序列化接口JsonSerializable > 1 接口功能方法 ~~~ JsonSerializable { ;指定要被序列化的数据 abstract public mixed jsonSerialize ( void ) } ~~~ > 2 接口使用实例 实例1 ~~~ <?php ;创建类实现序列化接口 class ArrayValue implements JsonSerializable { public function __construct (array $array ) { $this -> array = $array ; } public function jsonSerialize () { return $this -> array ; } } ;构造参数 $array = [ 1 , 2 , 3 ]; ;序列化输出json结构数据 echo json_encode (new ArrayValue ( $array ), JSON_PRETTY_PRINT ); ?> ~~~ 输出 ~~~ [ 1, 2, 3 ] ~~~ 实例2 ~~~ <?php ;创建类实现序列化接口功能 class ArrayValue implements JsonSerializable { public function __construct (array $array ) { $this -> array = $array ; } public function jsonSerialize () { return $this -> array ; } } ;构造参数 $array = [ 'foo' => 'bar' , 'quux' => 'baz' ]; ;序列化输出json结构数据 echo json_encode (new ArrayValue ( $array ), JSON_PRETTY_PRINT ); ?> ~~~ 输出 ~~~ { "foo": "bar", "quux": "baz" } ~~~