<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [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" } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看