PHP Predefined interfaces

Source: Internet
Author: User
Tags abstract foreach array object bool count interface valid
Introduction in PHP has several predefined interfaces, but also useful     iteratoraggregate (aggregated aggregate iterator iterator) Iteratoraggregate extends traversable {Abstract public traversable getiterator (void)} This interface implements a function--create an external iterator, specifically how to understand, when we use foreach to iterate over the object, If the Iteratoraggregate interface is not inherited, all the public properties in the object are traversed (only public $var this form). If you inherit iteratoraggregate, you'll use the object returned by the Getiterator method that is implemented in the class, where you should be aware that the return must be a Traversable object or an object that extends from traversable, or it throws an exception
See Example
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 ' => ' Yanru Itao ',
        ' C ' => ' LULU ',
    ];

    Public Function Getiterator ()
    {return
        new Arrayiterator ($this->_data);
    }
$obj = new My;
foreach ($obj as $key => $value) {
    echo "$key => $value \ n";
}
Result:
a => Yan
b => Yanruitao
c => LULU countable countable
{
abstract public int count (void)
}
This interface is used to count the number of objects, specifically how to understand, when we call count on an object, if the function does not inherit countable will always return 1, if inherited countable returns the number returned by the implemented Count method, look at the following example:
Class CountMe
{ 
    protected $_mycount = 3; 

    Public function count () 
    {return 
        $this->_mycount; 
    } 
} 

$countable = new CountMe (); 
echo count ($countable);
Returns 1

class CountMe implements countable
{ 
    protected $_mycount = 3; 

    Public function count () 
    {return 
        $this->_mycount; 
    } 
} 

$countable = new CountMe (); 
echo count ($countable);    
Returns 3

arrayaccess
arrayaccess {
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 function of this interface is to allow us to access the object like an array, how does this say, I guess is actually PHP in the lexical analysis of the time if you encounter an array of ways to use objects, go back to the object to find whether there is a implementation of arrayaccess if any, 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 method, see example below:
Class MyObj {} $obj = new MyObj;
$obj [' name ']; Fatal Error:cannot Use object of type MyObj as array into class MyObj implements Arrayaccess {public function off
    Setset ($offset, $value) {echo "Offsetset: {$offset} => {$value}\n";
    The Public Function offsetexists ($offset) {echo "offsetexists: {$offset}\n";
    The Public Function Offsetunset ($offset) {echo "Offsetunset: {$offset}\n";
    The Public Function Offsetget ($offset) {echo "Offsetget: {$offset}\n";
}} $obj = new MyObj;
$obj [1] = ' Yan ';
Isset ($obj [' name ']);
unset ($obj [' name ']);

$obj [' YRT '];  
Output results: offsetset:1 => Yan offsetexists:name offsetunset:name Offsetget:yrt class MyObj implements
    {Private $_data = [];
    Public Function Offsetset ($offset, $value) {$this->_data[$offset] = $value;
    The Public Function offsetexists ($offset) {return isset ($this->_data[$offset]);

 }   Public Function Offsetunset ($offset) {unset ($this->_data[$offset]);
    The 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 object above can only be a basic array operation, not even traversal,

    The combination of 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 => $value) {echo "{$key} => {$value}\n";}//output: YRT => Yan 1 => Yan name => Yan Age => Iterator iterator extends traversable {abstract public mixed current(void) abstract public scalar key (void) abstract public void next (void) abstract public void rewind (void) Abstract public boolean valid (void)}
An interface that can iterate over its own external iterator or class internally, it's an explanation from the official document, and it's hard to understand, but I feel like this interface implements functionality and Trratoraggregate (document: Creating an external iterator interface that interfaces directly back to an iterator) is similar, But this is implemented in the definition of the class itself, see 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 => false
1 => ' Yan '
This reference to the bird brother's article about a test (iterator mode), but the bird Brother's judgment valid a bit flawed, when the value of the North is false when the time will be truncated

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.