Welcome Guest, Not a member yet? Register   Sign In
The session class has a push method but no pop method
#1

It would be nice to have an inverse method to session::push, for example session::pop.
Reply
#2

remove or unset
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-27-2019, 09:43 AM)InsiteFX Wrote: remove or unset

 How do you want to apply remove or unset to session()->get('key')? You get a value back but not a reference.

session::push is equivalent to $_SESSION['key'][] = ....
session::pop would be equivalent to unset($_SESSION['key1']['key2'])
Reply
#4

Since the Session drivers work hand-in-hand with core PHP functionality, you don't need special features for this. You can modify $_SESSION directly. So what you've shown is perfect solution.

The only reason we've kept some of the old methods from prior versions of CI is to maintain some backward compatibility and some features like Flash so you don't have to manage it yourself. Otherwise, once the session is initialized with session() you can manipulate $_SESSION directly and it will use the driver you've chosen in the config file.
Reply
#5

(02-27-2019, 11:06 AM)kilishan Wrote: Since the Session drivers work hand-in-hand with core PHP functionality, you don't need special features for this. You can modify $_SESSION directly. So what you've shown is perfect solution.

The only reason we've kept some of the old methods from prior versions of CI is to maintain some backward compatibility and some features like Flash so you don't have to manage it yourself. Otherwise, once the session is initialized with session() you can manipulate $_SESSION directly and it will use the driver you've chosen in the config file.

I understand but for me it is more a question of programming style. When I have a push version I would also like to have a pop version. Otherwise I also do not need the push version. I have a setter and a getter method etc.

It is already the second time that I was astonished that I use session::get and session::has a few lines before and then I am doing the manipulation directly on $_SESSION. And after the second time I posted this message.
Reply
#6

That's fair. And there's a variety of working styles it supports.

session()->set() and session()->get() are the typical pair for usage. The session libs were pretty much a straight port from CI3 so I wasn't involved in that decision. I would guess a user wanted that ability and submitted a PR for it. We'd be open to a PR for pop, if you want to tackle it. Must support all of the current drivers, which should be relatively painless for something like this.

How often would you see that used? I don't think I've ever actually used a push on session data, to be honest. Typically I'd just grab the value from the session, make any modifications, then set it to the new value.
Reply
#7

1) Session::push is not part of SessionInterface, and I don't know that it should be relied on

2) Session::push as implemented is intended to merge additional array values into an existing session variable which has an array value. This does not sound like a conventional "push/pop" kind of thing, and I think the method is mis-named. A better name for the method might be "updateValues".

This does not look like a stack data structure, where push/pop make sense.
Reply
#8

(03-06-2019, 11:20 AM)ciadmin Wrote: 1) Session::push is not part of SessionInterface, and I don't know that it should be relied on

2) Session::push as implemented is intended to merge additional array values into an existing session variable which has an array value. This does not sound like a conventional "push/pop" kind of thing, and I think the method is mis-named. A better name for the method might be "updateValues".

This does not look like a stack data structure, where push/pop make sense.

Hi,

actually, I have chosen the name pop because it often as the opposite from push. I only prefer to have an undo function if there is a do function, especially if you cannot directly access the underlying array via the CI interface.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB