1,魔術方法__set與__get, __call >這些魔術方法,將在相關的屬性或者方法不存在時調用 >函數原型 .function __set( $property, $value ):傳遞屬性的名字和新的值 .function __get( $property ):傳遞屬性的名字,並且返回屬性 ...
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 }