php foreach parameter coercion type conversion problem _php tutorial

Source: Internet
Author: User
Tags php foreach rewind
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);

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