I'm trying out a recipe on how to store string resources in PHP, but I can't seem to get it to work. I am a little unsure on how the __get function works in relation to arrays and objects.
Error Message: "Fatal error: Cannot use object of type stdClass as array in /var/www/html/workspace/srclistv2/Resource.php on line 34"
What am I doing wrong?
/**
* Stores the res file-array to be used as a partt of the resource object.
*/
class Resource
{
var $resource;
var $storage = array();
public function __construct($resource)
{
$this->resource = $resource;
$this->load();
}
private function load()
{
$location = $this->resource . '.php';
if(file_exists($location))
{
require_once $location;
if(isset($res))
{
$this->storage = (object)$res;
unset($res);
}
}
}
public function __get($root)
{
return isset($this->storage[$root]) ? $this->storage[$root] : null;
}
}
Here is the resource file named QueryGenerator.res.php:
$res = array(
'query' => array(
'print' => 'select * from source prints',
'web' => 'select * from source web',
)
);
And here is the place I'm trying to call it:
$resource = new Resource("QueryGenerator.res");
$query = $resource->query->print;