PHP trying to create dynamic variables in classes
Solution 1
You could simply use the magic accessors to have as many instance attributes as you wish :
class test{
private $data;
public function __get($varName){
if (!array_key_exists($varName,$this->data)){
//this attribute is not defined!
throw new Exception('.....');
}
else return $this->data[$varName];
}
public function __set($varName,$value){
$this->data[$varName] = $value;
}
}
Then you could use your instance like this :
$t = new test();
$t->var1 = 'value';
$t->foo = 1;
$t->bar = 555;
//this should throw an exception as "someVarname" is not defined
$t->someVarname;
And to add a lot of attributes :
for ($i=0;$i<100;$i++) $t->{'var'.$i} = 'somevalue';
You could also initialize a newly created instance with a given set of attributes
//$values is an associative array
public function __construct($values){
$this->data = $values;
}
Solution 2
Try $this->{$varname}
class test
{
function __construct(){
for($i=0;$i<100;$i++)
{
$varname='var'.$i;
$this->{$varname}=$i;
}
}
}
BaneStar007
Full Stack Developer.. in training.. Just a few more layers to go.
Updated on June 22, 2022Comments
-
BaneStar007 about 2 years
I need to construct a class with alot of variables directly from the Database, For simplicity we'll name them 'userX', I've looked into ORM just a little, but its way over my head.
Essentially I thought I could use my procedural code
for ($i=0; $i<100; $i++) { public ${'user'.$i}; }
But, in a class
class test() { private $var1; for ($i=0; $i<10000; $i++) { public ${'user'.$i}; } function __constructor ..... }
Obviously not.. but it leaves me with the same problem, how can I add $user0, $user1, $user2, etc etc, without having to type all 10k of them in..
Obviously, it would be 1000x easier to just grab the names from the Database, but again, that looks even harder to code. Should I buckle down and grab them all ORM style?
-
Leonel Machava almost 12 yearsI am just complementing @ibtarek answer. You should implement these magical methods __set() and __get(). You should read __set() and __get() (for some reason SO is showing with just one underscore). __set() is called the first time that you attribute a value to a nonexistent or inaccessible property. __get() is called the first time that you try to read the value of an nonexistent or inaccessible property. You can learn more in this [SO thread](http:
-
BaneStar007 almost 12 yearsyour assigning the values from outside the class, I need to instantiate the variables in the class itself, will what yu wrote work for that? I'll give it a go.. nope: Parse error: syntax error, unexpected T_FOR, expecting T_FUNCTION
-
ibtarek almost 12 yearsIt should also work using "$this" in the class constructor $this->{'var'.$i} = 'value'.
-
BaneStar007 almost 12 yearsConfusion, maybe I have a brain bloackage, but in the constructor, your calling on variables which don't exist yet. I was under the impression that you had to declare the variables before your constructor.
-
ibtarek almost 12 yearsOr you could simply assign an associative array to $this->data. $this->data = array('var1'=>'val1','var2'=>'val2', .....);
-
ibtarek almost 12 yearsPHP does not require the existence of class attributes when you set them, but it trigers an error when you try to read a class attribute that is not defined yet.
-
BaneStar007 almost 12 yearsso, I can just ignore the get and set methods and put the variables in via the constructor.. sheesh, ok, blonde moment..