For Object Php

obatambeienwasirherbal.com: Object Iteration - Manual

Bạn đang xem: For object php

obatambeienwasirherbal.com: Object Iteration - Manual
obatambeienwasirherbal.com: Object Iteration - Manual
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes và Objects Namespaces Errors Exceptions Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces & Classes Context options & parameters Supported Protocols & Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apabít module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding obatambeienwasirherbal.com.. Keeping Current Features HTTPhường. authentication with obatambeienwasirherbal.com. Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Commvà line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting obatambeienwasirherbal.com"s Behaviour Audio Formats Manipulation Authentication Services Comm& Line Specific Extensions Compression & Archive sầu Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output đầu ra Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable và Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu vật phẩm k Previous thực đơn thành công g p Previous man page g n Next man page G Scroll to lớn bottom g g Scroll khổng lồ top g h Goto lớn homepage g s Goto search(current page) / Focus search box
Magic Methods »
« Overloading
obatambeienwasirherbal.comhường Manual Language Reference Classes & Objects
Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Romanian Russian Spanish Turkish Other
Submit a Pull Request Report a Bug

Object Iteration

obatambeienwasirherbal.comhường provides a way for objects khổng lồ be defined so it is possible to lớn iterate through a menu of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.


Example #1 Simple Object Iteration


class MyClass

    }
}

$class = new MyClass();

foreach(
$class as $key => $value
echo 
" ";


$class->iterateVisible();

?>

The above sầu example will output:


Xem thêm: Những Cung Hoàng Đạo Chiều Vợ Nhất, Mực, Ai Cũng Ham

var1 => value 1 var2 => value 2 var3 => value 3 MyClass::iterateVisible: var1 => value 1 var2 => value 2 var3 => value 3 protected => protected var private => private var

As the output shows, the foreach iterated through all of the visible properties that could be accessed.

To take it a step further, the Iterator interface may be implemented. This allows the object to lớn dictate how it will be iterated và what values will be available on each iteration.


Example #2 Object Iteration implementing Iterator


class MyIterator implements Iterator

    }

    public function 
rewind()
    
  
    public function 
current()
    
  
    public function 
key() 
    
  
    public function 
next() 
    
  
    public function 
valid()
    

}

$values = array(1,2,3);
$it = new MyIterator($values);

foreach (
$it as $a => $b
?>

The above example will output:


rewinding valid: 1 current: 1 key: 0 0: 1 next: 2 valid: 1 current: 2 key: 1 1: 2 next: 3 valid: 1 current: 3 key: 2 2: 3 next: valid:

The IteratorAggregate interface can be used as an alternative lớn implementing all of the Iterator methods. IteratorAggregate only requires the implementation of a single method, IteratorAggregate::getIterator() , which should return an instance of a class implementing Iterator .


Example #3 Object Iteration implementing IteratorAggregate


class MyCollection implements IteratorAggregate


    public function 
add($value
}

$coll = new MyCollection();
$coll->add("value 1");
$coll->add("value 2");
$coll->add("value 3");

foreach (
$coll as $key => $val
?>

The above sầu example will output:


rewinding current: value 1 valid: 1 current: value 1 key: 0 key/value: <0 -> value 1> next: value 2 current: value 2 valid: 1 current: value 2 key: 1 key/value: <1 -> value 2> next: value 3 current: value 3 valid: 1 current: value 3 key: 2 key/value: <2 -> value 3> next: current: valid:

Note:

For more examples of iterators, see the SPL Extension.

Note:

Generators provide an alternative sầu way of defining iterators.