2

(Sorry for my bad English, I use Google Translate)

I'm facing the problem of dynamically creating variables in the class. There are classes at the entrance to __construct transferred to variables.
Further cycles

foreach ($collector as $key => $field) {
    $this->$key = $field; 
} 

Variables are added to the class.
But this code works in other classes, except one. Here is the class

foreach ($collector as $key => $field) {
     $this->$key = $field;
            if (isset($this->$key))
              var_dump($this->$key);
        }
     var_dump('<pre>',$collector);
     var_dump($this);
     die();

var_dump('<pre>',$collector) - dumps the object

if (isset($this->$key))
var_dump($this->$key)

call magic __get(); since there is no variable

var_dump($this) - dumps the object but necessary variables I can not see.

Help me, please!

0

1 Answer 1

1

You could do something like this:

class MyClass
{
    private $_dynamic;

    function __get($name)
    {
        return $this->_dynamic[$name];
    }

    function __set($name, $value)
    {
        $this->_dynamic[$name] = $value;
    }
}
Sign up to request clarification or add additional context in comments.

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.