17

So for example I have this code:

class Object{
    public $tedi;
    public $bear;
    ...some other code ...
}

Now as you can see there are public variables inside this class. What I would like to do is to make these variables in a dynamic way, with a function something like:

private function create_object_vars(){

   // The Array what contains the variables
   $vars = array("tedi", "bear");

   foreach($vars as $var){
      // Push the variables to the Object as Public
      public $this->$var;
   }
}

So how should I create public variables in a dynamic way?

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213

3 Answers3

49
$vars = (object)array("tedi"=>"bear");

or

$vars = new StdClass();
$vars->tedi = "bear";
bfg9k
  • 491
  • 4
  • 2
  • @bfg9k please lend me a helping hand on this one http://stackoverflow.com/questions/42295574/how-to-convert-8209-array-object-variant-to-vt-variant – Joseph Feb 18 '17 at 13:29
9

Yes, you can do this.

You're pretty much correct - this should do it:

private function create_object_vars(){

   // The Array of names of variables we want to create
   $vars = array("tedi", "bear");

   foreach($vars as $var){
      // Push the variables to the Object as Public
      $this->$var = "value to store";
   }
}

Note that this makes use of variable variable naming, which can do some crazy and dangerous things!

As per the comments, members created like this will be public - I'm sure there's a way of creating protected/private variables, but it's probably not simple (eg you could do it via the C Zend API in an extension).

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • but in your snippet, there is no public , will it be public as default? – Adam Halasz Dec 05 '10 at 10:33
  • @CIRK - yes member variables created in this way will be public by default (in PHP 4 all members were public) - I'm not sure if there's a simple way of creating non-public variables. – John Carter Dec 05 '10 at 10:37
  • and what about the variable variables? what do you mean under this? – Adam Halasz Dec 05 '10 at 11:27
  • Well if you don't assign anything it wouldn't be a valid PHP statement. You could assign `null` if you want to create the variable but leave it empty. – John Carter Dec 05 '10 at 11:43
  • Yes, in `$this->$var`, `$var` is a variable variable, since it's using the *value* of `$var` for the variable name. Note that you can use this with similarly with local variables as `$$var`. – John Carter Dec 05 '10 at 11:47
  • But in general I'd try and avoid using variable variables unless you've got a good reason. – John Carter Dec 05 '10 at 11:48
6

As alternative, you can also derive your object from ArrayObject. So it inherits array-behaviour and a few methods which make injecting attributes easier.

class YourObject extends ArrayObject {

    function __construct() {
        parent::__construct(array(), ArrayObject::PROPS_AS_ARRAY);
    }

    function create_object_vars() {
        foreach ($vars as $var) {

            $this[$var] = "some value";

        }
    }

Attributes will then be available as $this->var and $this["var"] likewise, which may or not may suit the use case. The alternative method for setting attributes would be $this->offsetSet("VAR", "some value");.

Btw, there is nothing evil about variable variables. They're a proper language construct, as would be reusing ArrayObject.

mario
  • 144,265
  • 20
  • 237
  • 291