2

我有一个基类,它使用 php 魔术方法 __get 和 __set 能够修改扩展类中的私有属性。然后我为相关的私有属性构建了 setter getter 函数(类似于在这里找到的 http://www.beaconfire-red.com/epic-stuff/better-getters-and-setters-php

所以我的子类将如下所示:

class User extends BaseObject {
    public $id = -1;
    private $_status = "";


    function __construct($members = array()) {
        parent::__construct($members);

    }

    //Setter/Getter
    public function status($value = null) {
       if($value) {
           $this->_status = $value;

       } else {
           return $this->_status;
      }
}

现在,当我在基类中序列化这个作为 JsonSerialize 方法的对象时,序列化只会从子类中获取公共属性(即“Id”),但不会获取私有属性(即“_status”)这是序列化函数:

 public function jsonSerialize() {
    $json = array();
    foreach($this as $key => $value) {

            $json[$key] = $value;

    }
    return $json;
}

基类中的上述方法有什么方法可以识别子类中的所有Getter,以便它们可以包含在序列化中?换句话说,我希望序列化同时包含“id”和“status”

我意识到我可以获取类上的所有方法并使用某种命名约定来标识 getter/setter,但我特别需要保持 getter/setter 名称与属性名称相同,即 _status 必须有一个名为 status 的 getter setter () 那么还有其他方法可以识别这些特定功能吗?

4

3 回答 3

0

我将采用不同的方法......我认为我更喜欢它而不是反思。但我很想知道其他人是否同意。

我将根据访问时间在子类中注册每个 Getter/Setter,即将 getter/setter 名称存储在一个数组中。在序列化时,我不仅会对所有公共属性进行交互,还会对注册的 getter/setter 进行交互。

这是当我在我的基类中注册(存储 getter/setter 名称)时,将它存储在一个名为 _getters 的数组中。

function __set($name,$value){
    if(method_exists($this, $name)){
        $this->$name($value);
        //save in getters list for serialization
        if(!in_array($name,$this->_getters))
            array_push($this->_getters,$name);
    }
    else{
        // Getter/Setter not defined so set as property of object
        $this->$name = $value;
    }
}

现在在我的序列化中,我还检索任何注册的 getter 并将它们序列化。它运作良好!

    public function jsonSerialize() {
    $json = array();
    //do public properties

    foreach($this as $key => $value) {
            $json[$key] = $value;

    }

    //do any getter setters
    foreach($this->_getters as $key => $getter) {
        $value = $this->$getter;
        $json[$getter] = $value;

    }
    return $json;
}

这种方法有什么问题吗?

于 2018-02-06T20:16:18.333 回答
0

使用ReflectionReflectionProperty::setAccessible

    public function jsonSerialize()
    {
        $json = array();
        $class = new ReflectionClass($this);
        foreach ($class->getProperties() as $key => $value) {
            $value->setAccessible(true);
            $json[$value->getName()] = $value->getValue($this);

        }
        return $json;
    }

这是为了回答明确的问题。但是我不确定您是否应该使用此设计来解决您的问题。

于 2018-02-06T15:21:20.150 回答
0

也许有点不同的方法:

 public function toArray()
    {
        $descriptor = new \ReflectionClass(get_class($this));
        /** @var \ReflectionMethod[] $methods */
        $methods = $descriptor->getMethods(\ReflectionMethod::IS_PUBLIC);
        $array   = [];
        foreach ($methods as $method) {
            if (substr_compare('get', $method->getName(), 0, 3) === 0) {
                $property         = lcfirst(substr($method->getName(), 3));
                $value            = $method->invoke($this);
                $array[$property] = $value;
            }
        }

        return $array;
    }

除了返回数组,您还可以对其进行序列化。这种方法在子类上使用公共 getter 方法。

于 2019-01-31T13:39:26.763 回答