One thing I find myself often wanting to do is convert a large associative array, usually from a POST request (a registration form, for example, with many inputs) into local variables based on the key from the array. If you are using the values from the array often, your code quickly becomes littered with long variable names and quotes.
To prevent this for small associative arrays, it is acceptable to just do something like this:
$username = $_POST['username'];
$password = $_POST['password'];
I'm not escaping them for this example to keep everything as uncluttered as possible, so relax.
You can also do this:
list($username, $password) = $_POST;
But what if the $_POST array is larger? Then it becomes tedious to do both of those methods. To fix that, you can run a loop like this:
foreach($arr as $key => $value)
{
${$key} = $value;
}
The problem with this method is that it assigns local variables. It would be nice if it were possible to call a function in a parent class that runs this loop and those local variables were accessible within the calling class. Imagine an MVC setup where every controller is derived from a Controller
class, and likewise for the models. It would be nice to do:
$this->localize($_POST);
doWhatever($username) // $username works! Assuming $_POST['username'] is defined
Creating such a method causes the local variables to only remain within the scope of the localize()
function in the parent class, so this won't work. What I have been doing is running the same loop with one modification:
foreach($arr as $key => $value)
{
$this->{$key} = $value;
}
This works and all, but doesn't really solve the initial problem. Instead of code that is cluttered with brackets and quotes, it has $this->
all over the place, not to mention it is assigning variables that were never formally defined within the class.
So finally, my question: is it possible to create a function such as the localize()
I described, such that it can be inherited from a parent class but create local variables relative to the child class ($username
instead of $this->username
).
Also, whether you can or not, is this considered bad practice? It seems a bit hacky to me, and you are ignoring some principles of OOP. If it is, do you use a solution to fix the ugliness and clutter of large associative arrays, or do you just deal with it?
My Answer:
> $value) {
${$key} = $value;
}
This loop will iterate over each key-value pair in the associative array $arr and create a local variable with the key as the variable name and the corresponding value as its value. This way, you can easily access the values from the array using shorter variable names.
Here's an example of how you can use this loop with a larger associative array:
php
$_POST = array(
'username' => 'john_doe',
'password' => 'password123',
'email' => 'john.doe@example.com',
'age' => 25
);
foreach($_POST as $key => $value) {
${$key} = $value;
}
echo $username; // Output: john_doe
echo $password; // Output: password123
echo $email; // Output: john.doe@example.com
echo $age; // Output: 25
This way, you can easily convert a large associative array into local variables based on the keys, making your code cleaner and more readable.
Rate this post
3 of 5 based on 9803 votesComments