So, to prevent this information from appearing, when I use foreach, the arguments are forced to type conversions, the situation is as follows:
foreach (array) $arr as $key = = $value);
This has been peaceful, just a few days ago, suddenly there was a problem. I have forced the type conversion method to invoke object after it is not normal.
Copy CodeThe code is as follows:
Class Service implements iterator{
function __construct ($service _define, $filter =null) {
$this->iterator = new Arrayiterator ($service _define[' list ');
$this->filter = $filter;
$this->valid ();
}
function current () {
return $this->current_object;
}
Public Function Rewind () {
$this->iterator->rewind ();
}
Public Function key () {
return $this->iterator->current ();
}
Public function next () {
return $this->iterator->next ();
}
Public Function valid () {
while ($this->iterator->valid ()) {
if ($this->filter ()) {
return true;
}else{
$this->iterator->next ();
}
};
return false;
}
Private Function filter () {
$current = $this->iterator->current ();
if ($current) {
$this->current_object = new Sameple ($current);
if ($this->current_object) {
return true;
}
}
return false;
}
}
Class sameple{
var $class _name;
function __construct ($class _name = null) {
$this->class_name = $class _name;
}
Function Show () {
echo $this->class_name, '
';
}
}
$servicelist = Array (
' list ' = = Array (
' First ',
' Second ',
' Third ',
' Fourth ',
),
);
$ser = new Service ($servicelist);
foreach ($ser as $s) {
$s->show ();
}
/*
Code that executes the error uses the $ser to perform a forced type conversion operation
foreach ((array) $ser as $s) {
$s->show ();
}*/
The problem with this is that foreach can not only iterate through the array, but also traverse the class that implements the iterator interface.
I've only noticed the case of arrays, ignoring the class that implements the iterator interface. Will certainly pay attention to later.
To be remembered in turn.
http://www.bkjia.com/PHPjc/322584.html www.bkjia.com true http://www.bkjia.com/PHPjc/322584.html techarticle so, to prevent this information from appearing, when I use foreach, the arguments are forced to type, and the situation is as follows: foreach (array) $arr as $key = $value);