Is it possible to auto generate Getter/Setter from Array Values in PHP?
Posted
by Phill Pafford
on Stack Overflow
See other posts from Stack Overflow
or by Phill Pafford
Published on 2010-06-08T14:59:49Z
Indexed on
2010/06/08
15:02 UTC
Read the original article
Hit count: 130
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));
}
© Stack Overflow or respective owner