1. 程式人生 > >php面向對象高級-魔術方法與叠代器

php面向對象高級-魔術方法與叠代器

處理 each key square iterator arr val 實現 表示

1,魔術方法__set與__get, __call

>這些魔術方法,將在相關的屬性或者方法不存在時調用

>函數原型

  .function __set( $property, $value ):傳遞屬性的名字和新的值

  .function __get( $property ):傳遞屬性的名字,並且返回屬性的值

  .function __call( $methods, $args ):傳遞方法的名字和一個數字索引的數組,數組包含傳遞的參數,第一個參數的索引是0

 1 class Coordinate {
 2         private $arr = array
( ‘x‘ => null, ‘y‘ => null ); 3 function __get( $property ) { 4 if( array_key_exists( $property, $this->arr ) ){ 5 return $this->arr[$property]; 6 }else { 7 print "不能訪問一個不存在的鍵名" . PHP_EOL; 8 } 9 }
10 function __set( $property, $value ) { 11 if( array_key_exists( $property, $this->arr ) ) { 12 $this->arr[$property] = $value; 13 }else { 14 print "不能設置一個不存在的鍵名" . PHP_EOL; 15 } 16 } 17 } 18 19
$obj = new Coordinate(); 20 $obj->x = 10; 21 echo $obj->x . PHP_EOL; 22 $obj->n = 20; 23 echo $obj->n . PHP_EOL;

2,__call的應用

>通過__call可以使HelloWorldDelegator的實例調用HelloWorld的任意存在的方法

 1 class HelloWorld {
 2         function display( $count ){
 3             for( $i = 0 ; $i < $count; $i++ ) {
 4                 echo "Hello World $i " . PHP_EOL;
 5             }
 6             return $count;
 7         }
 8     }
 9 
10     
11     class HelloWorldDelegator {
12         private $obj;
13         function __construct(){
14             $this->obj = new HelloWorld();
15         }
16         function __call( $method, $args ) {
17             return call_user_func_array( array( $this->obj, $method ), $args );
18         }
19     }
20 
21     $obj = new HelloWorldDelegator();
22     print $obj->display( 3 );
23     print PHP_EOL;
24     
25     $obj->show();

3,叠代

>實現一個自己的叠代器,可以通過實現Iterator接口

Iterator接口原型:

Iterator extends Traversable {
/* Methods */
abstract public mixed current ( void )
abstract public scalar key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public bool valid ( void )
}
 1 class NumberSquared implements Iterator {
 2     private $start;
 3     private $end;
 4     private $cur;
 5 
 6     function __construct ( $start, $end ){
 7         $this->start = $start;
 8         $this->end = $end;
 9     }
10     function rewind(){ 
11         $this->cur = $this->start;
12     }
13     function key(){
14         return $this->cur;
15     }
16     function current(){
17         return pow( $this->cur, 2 );
18     }
19     function next(){
20         $this->cur++;
21     }
22     function valid(){
23         return $this->cur <= $this->end;
24     }
25 }    
26 
27 $obj = new NumberSquared( 3, 9 );
28 foreach( $obj as $key => $value ){
29     print "the square of $key is $value" . PHP_EOL;
30 }

類本身一般用來表示數據和擁有與這些數據的交互方法,所以一般不需要一個純粹的類叠代器,我們可以實現另一個接口:IteratorAggregate,接口摘要如下:

IteratorAggregate extends Traversable {
/* Methods */
abstract public Traversable getIterator ( void )
}

這個接口只有一個方法,作用是創建一個外部的叠代器接口

改造後的叠代,實現了叠代器與數據處理的分離:

 1 class NumberSquared implements IteratorAggregate {
 2     private $start;
 3     private $end;
 4 
 5     function __construct( $start, $end ) {
 6         $this->start = $start;
 7         $this->end = $end;
 8     }
 9 
10     function getIterator(){
11         return new NumberSquaredIterator( $this );
12     }    
13 
14     function getStart(){
15         return $this->start;
16     }
17 
18     function getEnd(){ 
19         return $this->end;
20     }
21 }
22 
23 class NumberSquaredIterator implements Iterator {
24     private $cur;
25     private $obj;
26     function __construct( $obj ) {
27         $this->obj = $obj;
28     }
29     function rewind(){ 
30         $this->cur = $this->obj->getStart();
31     }
32     function key(){
33         return $this->cur;
34     }
35     function next(){
36         $this->cur++;
37     }
38     function current(){
39         return pow( $this->cur, 2 );
40     }
41     function valid(){
42         return $this->cur <= $this->obj->getEnd();
43     }
44 }
45 
46 $obj = new NumberSquared( 3, 9 );
47 foreach( $obj as $key => $value ) {
48     print "the $key is $value " . PHP_EOL;
49 }

php面向對象高級-魔術方法與叠代器