0

Say I have a function called set_session_variable that looks like:

function set_session_variable($name, $value) {
    // ...write value to the specified path 
}

How would I write this function (without using an eval) so that I can do something like:

set_session_variable('foo', 'bar'); // Would set $_SESSION['foo'] = 'bar'; 
set_session_variable('foo[bar][baz]', 'blah'); // Would set $_SESSION['foo']['bar']['baz'] = 'blah';
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • 1
    Please explain, how do you want to retrieve foo. First you set foo to bar. At this point say `foo` is a string. Next you set `foo[bar][baz]` to blah. What behaviour do you expect at this point? Do you want to convert `foo` from string to array (such that `foo[bar][blaz]` has value blah)? Or $_SESSION['foo'] gives you array such that $_SESSION['foo']['bar']['baz'] == 'blah? – Ashwini Dhekane Feb 08 '12 at 15:02
  • I've updated my question to be a little more clear as to how I would like the $_SESSION variable to be written. At this point I'm not so much concerned w/ getting the variable as setting it. – Kyle Decot Feb 08 '12 at 15:06

3 Answers3

2

I highly suggest, that you won't use

set_session_variable('foo[bar][baz]', 'blah');

but instead

set_session_variable('foo', array('bar'=>array('baz' => 'blah')));

Additionally, you don't need a function call for that at all:

$_SESSION['foo']['bar']['baz'] = 'blah';

You can change the implementation of $_SESSION with the session save handler.

If you're only concerned how you could parse a string like 'foo[bar][baz]', this has been asked before, for example use strings to access (potentially large) multidimensional arrays.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • The whole point of my function is to abstract the whole process so that I don't have to interact w/ the $_SESSION variable directly – Kyle Decot Feb 08 '12 at 15:08
  • 1
    You only use a global function instead for which you even change the way to interact in a non-standard way (only to put `$_SESSION` in there, I'm pretty sure). Anyway, you see the second example-line of code and the linked question/answer. Both should fit your needs. – hakre Feb 08 '12 at 15:13
1

A more relevant question is why you need a function at all. Function calls have a cost, and the function doesn't appear to do useful work.

Example assignments:

$_SESSION['foo'] = 'bar';
$_SESSION['foo']['bar']['baz'] = 'blah';
$foo['bar']['baz'] = 'blah';
$_SESSION['foo'] = $foo;

In direct answer to your question: You could parse the value of $name within set_session_variable() using the PCRE module and a regular expression.

Even simpler and faster would be parsing it with sscanf() provided you are able and willing to impose a convention on the naming of array keys.

A cleaner alternative function:

$array['bar']['baz'] = 'blah';
set_session_variable('foo', $array);
function set_session_variable($key, $val) {
    $_SESSION[$key] = $val;
}
-1

One way to solve this is to mimic function overloading, example in this post -> PHP function overloading

Another way is to add one string argument to your function, with your array indices delimited. For example: set_session_variable('foo', 'bar', 'baz;key'); Which saves the value 'bar' into foo['baz']['key'].

All you have to do is tear the 3rd argument apart (i use ; as delimiter here).

Community
  • 1
  • 1
barticular
  • 46
  • 2