Phpforeach parameter forced type conversion problem

Source: Internet
Author: User
We all know that if the foreach parameter is not of the array type, a message similar to "Warning: Invalidargumentsuppliedforforeach () inXXX" warning will appear during running. Therefore, in order to prevent such information from appearing, when I use foreach, the parameter will be forcibly converted. The situation is as follows:
Foreach (array) $ arr as $ key => $ value );
In this case, the problem suddenly occurred a few days ago. After Forced type conversion, I cannot call the object method normally.
The 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 ();
}
/*
// The error code used to force type conversion for $ ser
Foreach (array) $ ser as $ s ){
$ S-> show ();
}*/

The reason for this problem is that foreach can not only traverse arrays, but also traverse classes that implement the Iterator interface.

Previously, I only noticed the array and ignored the class implementing the Iterator interface. Be sure to pay attention later.
In turn.

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.