I would like the access RequestParams::$headers
as an object.
So instead of:
RequestParams::$headers['something']
I can just do:
RequestParams::$headers->something;
I have the following in the construct that will take an array and convert it to an object:
public function __construct($array = null){
if (is_array($array)){
$data = array_change_key_case($array);
$object = json_decode(json_encode($data), FALSE);
$this->headers = $object;
}else if ($array === null){
//does not work
$this->headers = new \stdClass();
}
}
I then add objects with:
public function set ($key, $value){
if (!empty($key) && !empty($value)) {
return $this->headers->$key = $value;
}
}
However if there isn’t an array provided orignally (with data) I get a ’ Attempt to assign property of non-object’ error. I have worked round it by providing a default but whats the correct way to initialise it as an empty object?