So, in order to prevent such information from appearing, when I use foreach, I will force type conversion of the parameters, the situation is as follows:
foreach ((array) $arr as $key => $value);
This has been a peaceful thing, just a few days ago, suddenly there was a problem. I force a type conversion to not normally invoke the method of object.
Copy CodeThe code is as follows:
<?php
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, ' <br/> ';
}
}
$servicelist = Array (
' List ' => array (
' A ',
' Second ',
' Third ',
' Fourth ',
),
);
$ser = new Service ($servicelist);
foreach ($ser as $s) {
$s->show ();
}
/*
The code that executes the error uses the $ser to perform the coercion type conversion operation
foreach ((array) $ser as $s) {
$s->show ();
}*/
The problem with this is that foreach can not only traverse an array, but also iterate through the classes that implement the iterator interface.
I've only noticed the array situation before, ignoring the case of the class that implements the iterator interface. Be sure to pay attention in the future.
In turn, in the record.
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.