Henrique
Henrique

Reputation: 529

Creating dynamic static vars inside a class?

Is it possible to define dynamic vars or anyway to do the following?

Example 1:

class base
{
    protected static $$dynamicVar;

    protected function myFunction($value)
    {
        $dynamicVar = $value;
        self::$$dynamicVar = new $value();
    }
}

The idea behind this code is to instantiate new objects, without the base class knowing what objects will be instantiated.

I know instantiating new objects can be dynamic but I need it dynamic generated on static vars.

Thanks,

Upvotes: 0

Views: 102

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163238

This is not possible as far as I know, but you can always do something like this:

class base
{
   protected static $dynProps = array();

   protected function myFunction($value)
   {
      self::$dynProps[$value] = new $value();
   }
}

The more important question here is "Why do you need this?"

Upvotes: 2

Related Questions