Php object-oriented advanced-magic methods and iterators, php object-oriented magic

Source: Internet
Author: User
Tags rewind

Php object-oriented advanced-magic methods and iterators, php object-oriented magic

1. Magic Methods _ set and _ get, _ call

> These magic methods will be called when the relevant property or method does not exist.

> Function prototype

. Function _ set ($ property, $ value): Pass the attribute name and the new value

. Function _ get ($ property): Pass the property name and return the property value.

. Function _ call ($ methods, $ args): name of the method passed and an array of numeric indexes. The array contains the passed parameters. The index of the first parameter is 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 "a nonexistent key name cannot be accessed ". 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 "a nonexistent key name cannot be set ". 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 Application

> With _ call, the HelloWorldDelegator instance can call any existing methods of 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. Iteration

> Implement an Iterator of your own. You can implement the Iterator interface.

Iterator interface prototype:

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 }

The class itself is generally used to represent the data and the method of interaction with the data. Therefore, a pure class iterator is generally not required. We can implement another interface: IteratorAggregate. The abstract of the interface is as follows:

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

This interface has only one method to create an external iterator interface.

After the transformation, the iterator and data processing are separated:

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.