PHP pre-defined interface _php tutorial

Source: Internet
Author: User
Tags rewind

PHP Pre-defined interface


The introduction has several predefined interfaces in PHP and is useful for iteratoraggregate (converged aggregate iterators iterator) Iteratoraggregate extends traversable {abstract Public traversable getiterator (void)} This interface implements a function--to create an external iterator, how to understand it, when we use foreach to traverse the object, If the Iteratoraggregate interface is not inherited, all of the public properties in the object are traversed (only public $var this form). If the iteratoraggregate is inherited, the object returned by the Getiterator method implemented in the class will be used, it is important to note that the returned object must be a Traversable object or an extension from traversable, otherwise an exception will be thrown
See an example of class my{    private $_data = [        ' a ' = = ' Yan ',        ' b ' = ' Yanruitao ',        ' c ' = ' LULU ',    ];        Public Function Getiterator ()    {        return new Arrayiterator ($this->_data);}    } $obj = new My;foreach ($obj as $key + $value) {    echo "$key + = $value \ n";} The output is empty    class My implements Iteratoraggregate {    private $_data = [        ' a ' = ' = ' Yan ',        ' b ' and ' = ' Yanru Itao ',        ' c ' = ' LULU ',    ];    Public Function Getiterator ()    {        return new Arrayiterator ($this->_data);}    } $obj = new My;foreach ($obj as $key + $value) {    echo "$key + = $value \ n";} Results: a = Yan b = Yanruitaoc = lulucountablecountable {abstract public int count (void)}

This interface is used to count the number of objects, how to understand, when we call count on an object, if the function does not inherit countable will always return 1, if the inherited countable will return the implementation of the Count method returned by the number, look at the following example:
Class countme{     protected $_mycount = 3;     Public function count ()     {         return $this->_mycount;     }} $countable = new CountMe (); Echo Count ($countable) ;//Return 1class CountMe implements countable{     protected $_mycount = 3;     Public function count ()     {         return $this->_mycount;     }} $countable = new CountMe (); Echo count ($countable);    //returns 3ArrayAccessArrayAccess {abstract public boolean offsetexists (mixed $offset),    abstract public mixed Offsetget (mixed $offset) public    void Offsetset (mixed $offset, mixed $value) public    void Offsetunset (mixed $ Offset)}

The purpose of this interface is to allow us to access the object like an array, this is how to say, I guess in fact, PHP in the lexical analysis if encountered in the way the array of objects, go back to the object to find out if there is an implementation arrayaccess, if any, do the corresponding operation (set, unset, Isset, get) so that we can put an array inside the class, let the class implement the basic operation of the array mode, see an example:
Class myobj{} $obj = new myObj; $obj [' Name '];//fatal error:cannot use object of type MYOBJ as array in class MYOBJ Implemen    TS arrayaccess {public Function offsetset ($offset, $value) {echo "Offsetset: {$offset} = {$value}\n";    } Public Function offsetexists ($offset) {echo "offsetexists: {$offset}\n";    } Public Function Offsetunset ($offset) {echo "Offsetunset: {$offset}\n";    } Public Function Offsetget ($offset) {echo "Offsetget: {$offset}\n"; }} $obj = new myObj; $obj [1] = ' Yan '; isset ($obj [' name ']); unset ($obj [' name ']); $obj [' Yrt '];//output: offsetset:1 =    Yan OffsetExists:nameoffsetUnset:nameoffsetGet:yrtclass MYOBJ implements arrayaccess {private $_data = [];    Public Function Offsetset ($offset, $value) {$this->_data[$offset] = $value;    Public Function offsetexists ($offset) {return isset ($this->_data[$offset]); } Public Function Offsetunset ($offset) {UnseT ($this->_data[$offset]);    Public Function Offsetget ($offset) {return $this->_data[$offset]; }} $obj = new myObj; $obj [' yrt '] = ' Yan '; var_dump ($obj [' YRT ']); Var_dump (Isset ($obj [' YRT ']); unset ($obj [' YRT ']); var_ Dump (isset ($obj [' YRT ')); Var_dump ($obj [' YRT ']);//Output: string (9) "Yan" bool (TRUE) bool (false) notice:undefined index: YRT///The last one will report notice the above object can only be a basic array operation, not even traversal, combined with the previous iteratoraggregate can be Foreach:class MYOBJ implements Arrayaccess,    Iteratoraggregate{private $_data = [];    Public Function Getiterator () {return new Arrayiterator ($this->_data); }    ......} $obj = new myObj; $obj [' yrt '] = ' Yan '; $obj [1] = ' Yan '; $obj [' name '] = ' Yan '; $obj [' age '] = 23;foreach ($obj as $key + = $va Lue) {echo "{$key} = {$value}\n";} Output: YRT = Yan 1 = Yan name = Yan age = 23IteratorIterator extends traversable {abstract public mixed cur rent (void) abstract public scalar key (void), abstract public void next (void), abstract public void Rewind (vOID) Abstract public boolean valid (void)} 

The interface of an external iterator or class can be iterated internally, which is an explanation given by the official documentation, and it is not easy to understand, in fact, I feel that this interface implements the functionality and Trratoraggregate (document: Create an external iterator interface, the interface directly returns an iterator) similar to But this is in the definition of the class itself implemented, see an example:
Class MYOBJ implements iterator{    private $_data = [];    Public function __construct (Array $arr)    {        $this->_data = $arr;    }    Public function current ()    {        return current ($this->_data);    }    Public Function key ()    {        return key ($this->_data);    }    Public function Next ()    {        Next ($this->_data);    }    Public Function Rewind ()    {        reset ($this->_data);    }    Public function valid ()    {        return $this->key ()!== NULL;}    }  $t = [    ' yrt ' = ' Yan ',    ' name ' = ' Yan ',    false,    ' Yan ']; $obj = new MYOBJ ($t); foreach ($obj as $key = = $value) {    echo "{$key} =". Var_export ($value, true). " \ n ";} Output: YRT = ' Yan ' name = ' Yan ' 0 = false1 = ' Yan '

Above this reference to a bird brother of an article about a question (iterator mode), but the bird Brother's judgment valid a little flaw, when the value of the North is false when it will be truncated

http://www.bkjia.com/PHPjc/1039604.html www.bkjia.com true http://www.bkjia.com/PHPjc/1039604.html techarticle PHP pre-defined interface Introduction There are several predefined interfaces in PHP, and useful iteratoraggregate (converged aggregate iterator iterator) Iteratoraggregate extends Traversable ...

  • 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.