Re: advise on simplfying session usage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jan 12, 2012 at 7:16 PM, Haluk Karamete <halukkaramete@xxxxxxxxx> wrote:
> Again, coming from ASP background, I'm trying to minimize the typing
> for most needed functionalities..
>
> in asp, to set a session var, you go <%session("age")=90%> and to
> output it, you just go <%=session("age")%>
>
> in php, you've got to _SESSION['age']=90. that's a lot of keyboarding,
> lots of double key strokes and the entire word session has to be
> uppercase.

if you haven't (yet) disabled caps-lock, this is one thing it's
typically used for -- successful strings of capital letters.

Anyway, if you use $_SESSION[] a lot, then creating a short 2-char
function can be helpful. Someone else coming along later to maintain
your code might be highly mystified about it though.

> of course, if you use an IDE and you get fast at it, this may not be
> an issue but I wanted to simplify it anyway.
>
> so the plan is this
>
> <?php
>
> _s("age",43) //set the session var age to 43
> echo _s("age") //outputs the value
>
> ?>
>
> To achieve this; I wrote this preliminary function;
>
> function _s($var,$val = "r4r53d323,9e809023890j832e@14fdsffdd")
> {
>        if ($val == "r4r53d323,9e809023890j832e@14fdsffdd")
>        {return $_SESSION[$var];}
>        else
>        {$_SESSION[$var] = $val;}
> }

You should add in a check to make sure the $_SESSION[$var] actually
exists as an index in your first return statement and return a value
you can check against for success/failure.

>
> Now, what's that number you ask!... it's just a value which I figured
> I would never end up in a real app.
> It's just a way for me to use default argument of the function so I
> can call _s function with 1 or 2 arguments.
>
> Can this be done a better way? How do you use _s function with 1 or 2
> arguments so in 1 arg mode, you can use it as a set, and in 2 arg
> mode, you use it as a way to return val.
>
> Is func_get_args route the only way? performance wise which one would better?

This would be the safest way in this case. I'm not 100% how this would
devolve into byte-code, but my assumption is that there would be a
very slight performance cost, however, since you are calling a
function for every access to the $_SESSION array, you've already bit
the majority of that cost.

> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

All in all, I would not use this sort of aliasing of a function to an
array to save typing. Get an IDE if it really is that onerous. I can
type ses<tab> and it expands to $_SESSION['index'] automatically
dropping me at the first apostrophe with the whole inner string
highlighted. Saves *LOTS* of typing.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux