I have the following code example that works as I expected on PHP 5.5, but in the hosting server I have PHP 5.2.17 and it does not.
If I re run it several times, I get what I was expecting, the array gets bigger with different numbers.
But when I run it on PHP 5.2.17 I get the same number, as the instance of class A
is frozen.
Evenmore, If I remove this line $_SESSION['a'] = $a;
then it works on PHP 5.2.17, but it is not what I need. Anyone with knowledge of PHP releases could think of a way to manage this. Thanks
<?php
class A
{
public $var = 0;
}
session_start();
if(isset($_SESSION['a_array']))
{
$a = new A();
$_SESSION['a'] = $a;
$a->var = rand();
array_push($_SESSION['a_array'], $a);
}
else
{
$a_array = Array();
$_SESSION['a_array'] = $a_array;
}
var_dump($_SESSION['a_array']);
?>
I manage to find a dirty and bizzarre solution. I don't know if it fits other purposes, but at least I could continue with other stuff. Strangely it works if you change the name of the key of the $_SESSION array and add a by reference assignment, $_SESSION['a_aux'] = &$a;
. Worked on PHP 5.5 and PHP 5.2.17.
<?php
class A
{
public $var = 0;
}
session_start();
if(isset($_SESSION['a_array']))
{
$a = new A();
$a->var = rand();
array_push($_SESSION['a_array'], $a);
$_SESSION['a_aux'] = &$a;
}
else
{
$a_array = Array();
$_SESSION['a_array'] = $a_array;
}
var_dump($_SESSION['a_array']);
?>