0

I'm trying to update a value in session-Array, but it doesn't work. Initial set:

$bag = new SessionBag('p-' . $productId);
$bag->person = ['name' => 'john', 'age' => 25];

Then update:

$bag->person['age'] = 30;

After that the age is still 25 (checked in a xdebug-session).

Touheed Khan
  • 2,149
  • 16
  • 26
EugenA
  • 323
  • 3
  • 14

1 Answers1

1

If you enable warnings/notices on your web server, you will see something like "Notice: Indirect modification of overloaded property".

How to accomplish what you want?

$bag = new \Phalcon\Session\Bag('testest'); 
$bag->person = ['name' => 'john', 'age' => 25];
// $bag->person['age'] = 30; // Triggers Notice and will not work

$temp = $bag->person;
$temp['age'] = 44;
$bag->person = $temp;

print_r($bag);

[person] => Array ( [name] => john [age] => 44 )

If you are interested why this happens, you can read few explanations here PHP - Indirect modification of overloaded property

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
  • but it works for some people? https://forum.phalconphp.com/discussion/6015/storing-an-array-in-a-phalcon-session – EugenA Jun 02 '17 at 11:52
  • I guess it depends on php version or server settings, not sure friend :( – Nikolay Mihaylov Jun 02 '17 at 12:09
  • @EugenA I'm not sure it does. It sure hasn't for me in the last years I've been using Phalcon. I think that post you're linking to is showing how the person expects it to work, not how it actually works. Remember that you don't NEED to use Phalcon's session abstraction. You can just as easily use `$_SESSION` and since that's a superglobal array, you CAN do `$_SESSION['person']['age'] = 30` – Quasipickle Jun 07 '17 at 22:20