I have an issue with the following piece of code:
<?php
class testClass
{
public $settings;
public function __construct()
{
$this->settings = array(
'paths' => array(
'protocol' => 'http'
)
);
}
public function getSomething()
{
$string = "settings['paths']['protocol']";
echo $this->{$string}; /***** Line 19 *****/
}
}
$obj = new testClass;
$obj->getSomething(); // Outputs a 'undefined' notice
echo '<br />';
echo $obj->settings['paths']['protocol']; // Outputs http as expected
?>
This is a very basic example of the code I am employing, the actual code is more advanced, but the output / error produced is the same.
Basially, the class constructor populates a property with a settings array.
The getSomething() method assigns an array path to a variable, which is then attempted to be retrieved by the echo $this->{$string}; code.
When I write: $obj->getSomething(); I get the following error:
Notice: Undefined property: testClass::$settings['paths']['protocol'] in /test.php on line 19
If I write the following code echo $obj->settings['paths']['protocol'] I get the expected http
I'm not sure why this isn't working!! If anyone can shed any light, it would be greatly appreciated.
Thanks