Sto provando una ricetta su come memorizzare le risorse stringa in PHP, ma non riesco a farlo funzionare. Sono un po 'insicuro su come funziona la funzione __get in relazione a matrici e oggetti.__get risorsa in PHP "Impossibile utilizzare l'oggetto di tipo stdClass come array"
messaggio di errore: "Fatal error: Impossibile utilizzare oggetto di tipo stdClass come array in /var/www/html/workspace/srclistv2/Resource.php on line 34"
Che cosa sto facendo di sbagliato?
/**
* 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;
}
}
Ecco il file di risorse denominato QueryGenerator.res.php:
$res = array(
'query' => array(
'print' => 'select * from source prints',
'web' => 'select * from source web',
)
);
E qui è il luogo che sto cercando di chiamare:
$resource = new Resource("QueryGenerator.res");
$query = $resource->query->print;
Penso che questo funziona direttamente $ this-> stoccaggio -> $ radice –
@ElzoValugi Certo, lo fa. Lo uso perché penso che sia più comprensibile per i programmatori "non-php". – Leri
@PLB: l'uso di questa funzione restituisce NULL (dalla parte "else" del controllo). Ancora con "$ risorsa-> query-> stampa" come se fosse uno scalare con una stringa. –