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?

2
  • Sorry @Pekka , I though it's clear but I attached it to the end :) Commented Dec 5, 2010 at 10:30
  • Out of curiosity, why are you doing this? Commented Dec 5, 2010 at 12:24

3 Answers 3

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

or

$vars = new StdClass();
$vars->tedi = "bear";
Sign up to request clarification or add additional context in comments.

1 Comment

@bfg9k please lend me a helping hand on this one stackoverflow.com/questions/42295574/…
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).

6 Comments

but in your snippet, there is no public , will it be public as default?
@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.
and what about the variable variables? what do you mean under this?
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.
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.