Is it possible to auto generate Getter/Setter from Array Values in PHP?
- by Phill Pafford
So I have a couple of arrays
$array_1 = Array('one','two','three');
$array_2 = Array('red','blue','green');
Is there a dynamic way to create the Setters and Getters for an array with single value entries?
So the class would be something like:
class xFromArray() {
}
So the above if I passed $array_1 it would generate something like this:
private $one;
setOne($x) {
$one = $x;
}
getOne() {
return $one;
}
if I passed $array_2 it would generate something like this:
private $red;
setRed($x) {
$red = $x;
}
getRed() {
return $red;
}
So I would call it somehow like this? (My best guess but doesn't seem that this would work)
$xFromArray = new xFromArray;
foreach($array_1 as $key=>$data) {
$xFromArray->create_function(set.ucfirst($data)($data));
echo $xFromArray->create_function(get.ucfirst($data));
}