PHP事前定義インタフェースの紹介


PHPは6つのインタフェースを定義して以下のように紹介した:1.Traversableはインタフェースを遍歴してほほほ!実際にはPHPで使用できるインタフェースではなく、内部クラスが使用できます.クラスが遍歴できるかどうかを検出する用途があります.
if($class instanceof Traversable) {    //foreach}

 2.Iterator反復インタフェースの概要:
Iterator extends Traversable  {      //                   abstract public mixed current(void)      //                      abstract public scalar key(void)      //                    abstract public void next(void)      //                    abstract public void rewind(void)      //                  ,      rewind()  next()        abstract public boolean valid(void)  }

上記のように、1つのクラスに基本的な反復機能を実装します.反復の呼び出し順序は次のように表示されます.
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( $it  as  $key  =>  $value ) {    var_dump ( $key ,  $value );    echo  "
" ;}

3.IteratorAggregate集約反復インタフェースインタフェースの概要:
IteratorAggregate  extends Traversable  { //       abstract public Traversable getIterator  ( void )}

getIteratorは、IteratorまたはTraversableインタフェースのクラスのインスタンスです.反復アクセスを実現する外部反復器を次のように取得します.
class  myData  implements  IteratorAggregate  {    public  $property1  =  "Public property one" ;    public  $property2  =  "Public property two" ;    public  $property3  =  "Public property three" ;     public function  __construct () {        $this -> property4  =  "last property" ;    }         public function  getIterator () {        return new  ArrayIterator ( $this );    }} $obj  = new  myData ; foreach( $obj  as  $key  =>  $value ) {    var_dump ( $key ,  $value );    echo  "
" ;}

4.ArrayAccess配列式アクセスインタフェースインタフェースの概要:
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  ) //          }

次に、アクセス配列のようにオブジェクトにアクセスします.
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 );

5.Serializableシーケンス化インタフェースの概要:
Serializable  {     /*    */    abstract public string serialize  ( void ) //            abstract public mixed unserialize  ( string $serialized  ) //     }

このインタフェースを実装するクラスはサポートされていません.sleep()と_wakeup().オブジェクトをシーケンス化するとserializeメソッドが呼び出され、逆シーケンス化するとunserializeメソッドが呼び出されます.
class  obj  implements  Serializable  {    private  $data ;    public function  __construct () {        $this -> data  =  "My private data" ;    }    public function  serialize () {        return  serialize ( $this -> data );    }    public function  unserialize ( $data ) {        $this -> data  =  unserialize ( $data );    }    public function  getData () {        return  $this -> data ;    }} $obj  = new  obj ;$ser  =  serialize ( $obj );print_r($ser);$newobj  =  unserialize ( $ser );print_r($newobj);

6.Closureインタフェースの概要:
Closure  {    /*    */    __construct  ( void ) //                public static Closure bind  ( Closure  $closure  , object $newthis  [, mixed  $newscope  = 'static'  ] ) //      ,     $this       。    public Closure bindTo  ( object $newthis  [, mixed  $newscope  = 'static'  ] ) //        ,     $this       。}
class  A  {    private static  $sfoo  =  1 ;    private  $ifoo  =  2 ;} $cl1  = static function() {    return  A :: $sfoo ;}; $cl2  = function() {    return  $this -> ifoo ;};  $bcl1  =  Closure :: bind ( $cl1 ,  null ,  'A' ); $bcl2  =  Closure :: bind ( $cl2 , new  A (),  'A' );echo  $bcl1 (),  "
" ;echo  $bcl2 (),  "
" ;

転載は住所を明記してください.http://www.phpddt.com/php/php-interfaces.html他人の労働成果を尊重することは自分を尊重することだ.