set_from_array_unsafe($array)

if $array is not an array: return false

Attempts to set class variables with names coinciding with the array indexes and values coinciding with their respective values.

If the setter set_"array index" doesn't exist (this class variable doesn't exist), the class variable will be created and the value will be set.

If the setter set_"array_index" does exist, the value must meet the validation requirements. If it fails to, the function will return false

If there were no issues return true

function code:
<?php
    
function set_from_array_unsafe($array '')
    {
        if (@!
is_array($array))
            return 
false;
        foreach(
$array as $k=>$v)
        {
            if (@
method_exists($this"set_$k"))
            {
                if (@!
$this->{"set_$k"}($v))
                    return 
false;
            }
            else
                
$this->{$k} = $v;
        }
        return 
true;
    }

?>



Example:

example.php
<?php
include("lib.php");
load("person");

if (
$array $_POST["array"])
{
    
$p = new person;
    if (!
$errors $p->set_from_array_unsafe($array))
    {
        echo 
"There were errors";
        return 
0;
    }
    else
    {
        ...
    }
}

?>


<form method=post action=example.php>
Name:<input type=text name=array[name]><br>
Address:<input type=text name=array[address]><br>
City:<input type=text name=array[city]><br>
State:<input type=text name=array[state]><br>
Age:<input type=text name=array[age]><br>
Birthdate:<input type=text name=array[birthdate]><br>
Mother:<input type=text name=array[mother]><br>
Father:<input type=text name=array[father]><br>
<input type=submit value=Submit>
</form>