Original Title: How can I dynamically enclose attributes in variable before actual object call
Generic Question
How can I create $target in a way that it can be correctly var_dumped?
$type = 'lib';
$target = 'test->test2';
var_dump($GLOBALS[$this->context]->$type->test->test2);//returns object(test\test2)#15 (0) { }
var_dump($GLOBALS[$this->context]->$type->{$target}); //returns NULL ( Undefined property: stdClass::$test->test2 )
More Examples
this (below) works like a charm
$target = 'test';
$type = new \stdClass();
$type->test = new \stdClass();
$type->test->test2 = 5;
var_dump($type->$target); // Returns object(stdClass)#24 (1) { ["test2"]=> int(5) }
this (below) does not :
$target = 'test->test2';
$type = new \stdClass();
$type->test = new \stdClass();
$type->test->test2 = 5;
var_dump($type->$target);// Returns NULL (Notice: Undefined property: stdClass::$test->test2)
Real Case :
I want to unset $GLOBALS[$this->context]->$type->test->test2
My first though :
public function unSys($type, $thing) {
//$type = 'lib';
//$thing = 'test/test2';
$parts = explode('/',$thing);
$final = implode('->',$parts);
unset($GLOBALS[$this->context]->$type->{$final});
}
What I've tried after that :
...
$parts = explode('/',$thing);
$target = $GLOBALS[$this->context]->$type;
foreach ($parts as $value) {
$target = $target->$value;
}
unset($target);
var_dump($GLOBALS[$this->context]->$type->test->test2);//still exist
...
I also tried passing by reference without luck :
...
$target = &$GLOBALS[$this->context]->$type;
...
call_user_func()is your friend