I’m trying to define the following function:
function set($key, $value, $default = null)
{
if ($value)
static $$key = $value;
else
static $$key = $default;
}
But when I call it, I get the error “Parse error: syntax error, unexpected ‘$’, expecting T_VARIABLE”. I guess there is a problem with defining static variable variables, but I can’t find a workaround. Can you? Thanks!
If I really wanted to do something like that, I’d use a static array instead:
If you want to also be able to access this data, a better idea is to encapsulate it in a class.
Then you can use the class statically:
An even better way of doing this is to not use the
statickeyword at all, and instead make a normal class with normal properties, then make a single instance of it and use that instance.You can then make an instance of the class and pass it along: