Edit report at https://bugs.php.net/bug.php?id=68240&edit=1
ID: 68240
Updated by: [email protected]
Reported by: 498936940 at qq dot com
Summary: php class vars have a bug in foreach
-Status: Open
+Status: Not a bug
Type: Bug
Package: PHP Language Specification
Operating System: WIN 8.1
PHP Version: 5.6Git-2014-10-16 (snap)
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
You are overwriting $this->s['a'] in the first iteration of foreach to a1e. And as
in a1e there's no %s, it remains unchanged in further iterations.
Previous Comments:
------------------------------------------------------------------------
[2014-10-16 09:11:02] 498936940 at qq dot com
Description:
------------
class a{
public $s = array('a' => 'a%se');
function aa(){
foreach(range(1,5) as $v){
$this->s['a'] = str_replace('%s', $v, $this->s['a']);
var_dump($this->s);
}
}
}
$a = new a();
$a->aa();
result always:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
i think right:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a2e"
}
array(1) {
["a"]=>
string(3) "a3e"
}
array(1) {
["a"]=>
string(3) "a4e"
}
array(1) {
["a"]=>
string(3) "a5e"
}
Test script:
---------------
class a{
public $s = array('a' => 'a%se');
function aa(){
foreach(range(1,5) as $v){
$this->s['a'] = str_replace('%s', $v, $this->s['a']);
var_dump($this->s);
}
}
}
$a = new a();
$a->aa();
result always:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
i think right:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a2e"
}
array(1) {
["a"]=>
string(3) "a3e"
}
array(1) {
["a"]=>
string(3) "a4e"
}
array(1) {
["a"]=>
string(3) "a5e"
}
Expected result:
----------------
class a{
public $s = array('a' => 'a%se');
function aa(){
foreach(range(1,5) as $v){
$this->s['a'] = str_replace('%s', $v, $this->s['a']);
var_dump($this->s);
}
}
}
$a = new a();
$a->aa();
result always:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
i think right:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a2e"
}
array(1) {
["a"]=>
string(3) "a3e"
}
array(1) {
["a"]=>
string(3) "a4e"
}
array(1) {
["a"]=>
string(3) "a5e"
}
Actual result:
--------------
class a{
public $s = array('a' => 'a%se');
function aa(){
foreach(range(1,5) as $v){
$this->s['a'] = str_replace('%s', $v, $this->s['a']);
var_dump($this->s);
}
}
}
$a = new a();
$a->aa();
result always:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a1e"
}
i think right:
array(1) {
["a"]=>
string(3) "a1e"
}
array(1) {
["a"]=>
string(3) "a2e"
}
array(1) {
["a"]=>
string(3) "a3e"
}
array(1) {
["a"]=>
string(3) "a4e"
}
array(1) {
["a"]=>
string(3) "a5e"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68240&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.