I am currently creating a class to handle all my form data which will then be placed into a database for future use.
Currently in my class constructor I am using the isset() function on an individual basis when I construct my variables from POST.
function __construct()
{
if (isset($_POST['first_name']))
{
$this->first_name = $_POST['first_name'];
}
else
{
$this->first_name = "NULL";
}
}
Although this example is just for one variable, I am doing this for another 12 in my php application.
As the old adage says “You cannot repeat code”.
So I am trying to create a member function in my class that will be able to handle all variables, like a get/set function.
So that in my constructor all I need to do is this:
function __construct()
{
//first parameter is the member variable to be set, second parameter is the element name that the value will be POST from
$firstname = $this->setter($firstname, "first_name");
}
//what do I put here?
function setter(first parameter, second parameter)
{
//checks the post variable has been set
if ( isset($_POST[second_parameter]))
return $_POST['second_parameter'];
else
return "NULL";
}
Can someone point me in the right direction, or with a code sample.
The aspect I am not sure about is how do I set the function to handle two parameters with a generic handle?
Thanks!
first of all, this is a sytax error (I guess you know)
also, this should issue an warning and lead to other behavior than what you want
you are using an undefined constant that after the warning evaluates to the string “second_parameter”, you should use the variable
also, here you are using a string and not a variable:
another problem is the fact that you return the values, and you don’t set them as members in your class
so maybe something like
might be closer to what you need