$this->fields['somefield'] = $someVal;
public function __get($name){
if($name == 'f'){
return (object) $this->fields;
}
else return false;
}
<?php
class My {
private $fields = [
'hello' => 'world'
];
public function f() {
return $this;
}
public function __get($name) {
if($name == 'f') {
return (object) $this->fields;
}
}
public function __set($name, $value) {
$this->fields[$name] = $value;
}
public function printFields() {
print_r($this->fields);
}
}
$my = new My();
$my->f()->jetpack = 'azaza';
$my->printFields();
<?php
class Fields
{
protected $parent;
public function __construct(\Data &$parent)
{
$this->parent = $parent;
}
public function __get($name)
{
return array_key_exists($name, $this->parent->fields) ? $this->parent->fields[$name] : false;
}
public function __set($name, $value) {
$this->parent->fields[$name] = $value;
}
}
class Data
{
public $fields = [
'foo' => "bar"
];
public function __get($name){
if($name == 'f'){
return new \Fields($this);
}
else {
return false;
}
}
}
$data = new Data();
$data->f->testInfo = "test passed";
echo $data->fields['testInfo']; // выведет "test passed"
$this->f->somefield красиво отдает из массива значение по ключу.
class Shit
{
private $fields = [];
public function __get($name){
if($name !== 'f') {
return false;
}
return new class($this->fields) {
private $fields;
public function __construct(&$fields)
{
$this->fields = &$fields;
}
public function __get($name)
{
return $this->fields[$name];
}
public function __set($name, $value)
{
$this->fields[$name] = $value;
}
public function __isset($name)
{
return isset($this->fields[$name]);
}
};
}
}