| Author |
PHP5, foreach and memory corruption
|
|
|
| Hi!
I have just fixed my CMS so it works on PHP5 (it worked without problems on
PHP4 but gave weird errors on PHP5). The problem lied in this construct:
echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
foreach ($this->someLargeArrayWithObjects as $key=>$value)
{
$a=$value['object']->someFunction();
};
echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs _1_
(one)!
This is of course pseudo-code - and no, function was not changing anything.
This solved the problem:
echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
$keys=array_keys($this->someLargeArrayWithObjects);
foreach ($keys as $key)
{
$value=&$this->someLargeArrayWithObjects[$key];
$a=$value['object']->someFunction();
};
echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs 11
Has anybody else had these kind of problems? The zend.ze1_compatibility_mode
directive didn't help...
I solved it now and it might even be more effective (memory-wise), but still
- I'd like to know if it's me or something else.
Another thing: is there a nicer way to traverse an array without copying the
values? All of the foreach(), current(), next() and similar do that - but I
want to traverse an array just by referencing the values.
Regards,
Anze
| |
|
| > Another thing: is there a nicer way to traverse an array without copying
> the values? All of the foreach(), current(), next() and similar do that -
> but I want to traverse an array just by referencing the values.
Of course, I'm looking for solution that would work in PHP4 too - in PHP5
you can use foreach with '&' before $value.
Regards,
Anze
| |
| Stefan Rybacki 2005-08-21, 9:55 pm |
| Anze wrote:
>
>
> Of course, I'm looking for solution that would work in PHP4 too - in PHP5
> you can use foreach with '&' before $value.
>
And you can't use & in PHP4?
Stefan
> Regards,
>
> Anze
| |
|
| >> Of course, I'm looking for solution that would work in PHP4 too - in PHP5
> And you can't use & in PHP4?
No - documentation for foreach construct says:
"As of PHP 5, you can easily modify array's elements by preceding $value
with &. This will assign reference instead of copying the value. "
I guess that means I can't use that in PHP4... A lot of my code runs on PHP4
so I must be sure I don't make incompatible changes.
Regards,
Anze
| |
| Stefan Rybacki 2005-08-22, 7:55 am |
| Anze wrote:
>
>
> No - documentation for foreach construct says:
>
> "As of PHP 5, you can easily modify array's elements by preceding $value
> with &. This will assign reference instead of copying the value. "
>
> I guess that means I can't use that in PHP4... A lot of my code runs on PHP4
> so I must be sure I don't make incompatible changes.
>
> Regards,
Ok I see.
Stefan
>
> Anze
| |
| Kleist 2005-08-22, 7:55 am |
| Anze wrote:
>
> Another thing: is there a nicer way to traverse an array without copying the
> values? All of the foreach(), current(), next() and similar do that - but I
> want to traverse an array just by referencing the values.
>
http://php.paco.net/manual/en/langu....iterations.php
this might help
| |
| Colin McKinnon 2005-08-22, 7:55 am |
| Anze wrote:
> Hi!
>
> I have just fixed my CMS so it works on PHP5 (it worked without problems
> on PHP4 but gave weird errors on PHP5).
<snip>
> This solved the problem:
>
> echo ("BEFORE: ".count($this->someLargeArrayWithObjects)); // outputs 11
> $keys=array_keys($this->someLargeArrayWithObjects);
> foreach ($keys as $key)
> {
> $value=&$this->someLargeArrayWithObjects[$key];
> $a=$value['object']->someFunction();
> };
> echo ("AFTER: ".count($this->someLargeArrayWithObjects)); // outputs 11
>
It doesn't really solve the problem but did you try:
foreach ($this->someLargeArrayWithObjects as $key=>$value)
{
$a=someLargeArrayWithObjects['key']['obj
ect']->someFunction();
}
C.
| |
| Hilarion 2005-08-22, 7:55 am |
| > It doesn't really solve the problem but did you try:
>
> foreach ($this->someLargeArrayWithObjects as $key=>$value)
> {
> $a=someLargeArrayWithObjects['key']['obj
ect']->someFunction();
> }
Probably you meant:
$a=someLargeArrayWithObjects[ $key ]['object']->someFunction();
Hilarion
| |
|
| > $a=someLargeArrayWithObjects[ $key ]['object']->someFunction();
Yes, I did - just a typo, I have it right in the code.
Regards,
Anze
| |
|
| > Probably you meant:
>
> $a=someLargeArrayWithObjects[ $key ]['object']->someFunction();
I see, it wasnt MY typo... ;)
Colin: I did try this but it didn't change anything.
Regards,
Anze
| |
|
| Kleist wrote:
> Anze wrote:
>
>
> http://php.paco.net/manual/en/langu....iterations.php
>
> this might help
Thanks, though I was hoping for PHP4 & PHP5 compatible solution - my code is
being hosted on different providers.
I looked around and I think there is no such thing in PHP4 - pity.
Regards,
Anze
| |
| Hilarion 2005-08-22, 6:58 pm |
| > > Probably you meant:
>
> I see, it wasnt MY typo... ;)
True :) Sorry.
Hilarion
| |
| Norman Peelman 2005-08-22, 6:58 pm |
| "Anze" <anzenews@volja.net> wrote in message
news:yDoOe.1653$cE1.234341@news.siol.net...
> Kleist wrote:
copying[color=darkred]
that -[color=darkred]
>
> Thanks, though I was hoping for PHP4 & PHP5 compatible solution - my code
is
> being hosted on different providers.
> I looked around and I think there is no such thing in PHP4 - pity.
>
> Regards,
>
> Anze
You could always do it both ways based on the 'phpversion()' function or
PHP_VERSION constant...
$version = PHP_VERSION;
if ($version[0] == '4')
{
php4 version here
}
elseif ($version[0] == '5')
{
php5 version here
}
Norm
---
FREE Avatar hosting at www.easyavatar.com
|
|
|
|