4

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?

BaneStar007
  • 377
  • 1
  • 4
  • 15
  • Download the Magento open source project and try to read the code in the Varien_Object class. It will give you a fairly good idea on how achieve your goal and what to google for. – Mihai Todor Sep 08 '12 at 12:59

3 Answers3

19

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;
}
ibtarek
  • 795
  • 4
  • 16
  • your 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 – BaneStar007 Sep 08 '12 at 12:45
  • 2
    It should also work using "$this" in the class constructor $this->{'var'.$i} = 'value'. – ibtarek Sep 08 '12 at 12:55
  • Confusion, 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. – BaneStar007 Sep 08 '12 at 13:00
  • Or you could simply assign an associative array to $this->data. $this->data = array('var1'=>'val1','var2'=>'val2', .....); – ibtarek Sep 08 '12 at 13:02
  • 1
    PHP 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. – ibtarek Sep 08 '12 at 13:04
  • so, I can just ignore the get and set methods and put the variables in via the constructor.. sheesh, ok, blonde moment.. – BaneStar007 Sep 08 '12 at 13:24
  • I am just complementing @ibtarek answer. You should implement these magical methods [__set()](http://www.php.net/manual/en/language.oop5.overloading.php#object.set) and [__get()](http://www.php.net/manual/en/language.oop5.overloading.php#object.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: – Leonel Machava Sep 08 '12 at 11:47
3

Try $this->{$varname}

class test
{

    function __construct(){

       for($i=0;$i<100;$i++)
       {

         $varname='var'.$i;
         $this->{$varname}=$i;
       }
    }
}
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
0

You can use variable variables ($$var) - content of one variable is used as a name for other variable (double $$)

Therefore not $this->varname but $this->$varname.

class test
{
   for($i=0;$i<100;$i++)
   {
     $varname='var'.$i;
     $this->$varname=$i;
   }
}

This will dynamically create 100 variables with names $var0, $var1 ...

sbrbot
  • 6,169
  • 6
  • 43
  • 74