php类的多功能getter

php类的多功能getter,php,php-7.2,Php,Php 7.2,我编写了某种安全类,它接收stdClass类型的内容: class SafeContent extends stdClass { ​ /** @var stdClass $content */ protected $content; ​ public function __construct(stdClass $content) { $this->content = $content; } ​ public function __ge

我编写了某种安全类,它接收
stdClass
类型的内容:

class SafeContent extends stdClass {
​
    /** @var stdClass $content */
    protected $content;
​
    public function __construct(stdClass $content) {
        $this->content = $content;
    }
​
    public function __get($name) {
        if ($this->content->name == null) {
             // do something
        }
    }
​
}

我想让
SafeContent
的getter捕捉像
$SafeContent->someField->anotherField->lastField
这样的get操作,所以
\u get
中的
name
参数将是
someField->anotherField->lastField
或类似的东西。有办法管理吗?

没有办法用一个参数获取一个
\uu get
中的所有字段,但是您可以将每个对象强制转换为
安全内容
类,因此每个顺序调用都将进入类的“getter:

// inside __get function
if (!isset($this->content->$name) || $this->content->$name == null) {
    // do something, probably throw an exception
}

if ($this->content->$name instanceof stdClass) {
    // return the instance of your class with appropriate content
    $component = new self($this->content->$name);
    return $component;
}

return $this->content->$name;

无法用一个参数获取一个
\u get
中的所有字段,但您可以将每个对象强制转换为
安全内容
类,因此每个顺序调用都将输入类的“getter:

// inside __get function
if (!isset($this->content->$name) || $this->content->$name == null) {
    // do something, probably throw an exception
}

if ($this->content->$name instanceof stdClass) {
    // return the instance of your class with appropriate content
    $component = new self($this->content->$name);
    return $component;
}

return $this->content->$name;

您实际上不需要扩展
stdClass
;这已经是一个隐式的事实,因为它是一个类。您实际上不需要扩展
stdClass
;这已经隐含在它是一个类的事实中了。