简体   繁体   English

克隆PHP对象并在克隆上设置受保护的属性

[英]Clone PHP object and set protected property on clone

I got a logical problem. 我有一个逻辑问题。

I got an object that needs to be cloned. 我有一个需要克隆的对象。

The object is|has a result of a calculation. 目的是计算的结果。

The object has a runtime. 该对象具有运行时。

In some cases it is faster to clone the object instead of calculating the result again 在某些情况下,克隆对象比重新计算结果更快

(fe same parameter ^= same result) . (具有相同的参数^ =相同的结果)

But the runtime must not be copied. 但是 不能复制运行时。

The runtime would be the time to determine that i can use the same result (object). 运行时将是确定我可以使用相同结果(对象)的时间。

Example: 例:

class Object
{
    protected $runtime;

    public function getRuntime()
    {
        return $this->runtime;
    }

    public function doSome(/*...*/)
    {
        $start = microtime(true);
        // ... the heavy work ...
        // ...
        $this->runtime = microtime(true) - $start;
    }
}

$objects = [];
while (/*...*/) {
    if (count($objects) > 0) {
        $start = microtime(true);
        if (/*check if would get the same result as the previous one*/) {
            $object = clone end($objects);
            // MUST change the runtime here on the clone 
            // but i should not make :runtime public
            $object->runtime = microtime(true) - $start; // :(
            $objects[] = $object;
            continue;
        }
    }
    $object = new Object();
    $object->doSome(/*...*/);
    $objects[] = $object;
}

How could i clone the previous object and set the actual runtime on the clone without making it possible to make the runtime property public? 我如何克隆先前的对象并在克隆上设置实际的运行时,而又无法将运行时属性设为公开?

I would suggest to put this logic in separated method Object::clone() like this: 我建议将此逻辑放在这样的分离方法Object::clone()

class Object
{
    protected $runtime;

    public function getRuntime()
    {
        return $this->runtime;
    }

    public function doSome(/*...*/)
    {
        $start = microtime(true);
        // ... the heavy work ...
        // ...
        $this->runtime = microtime(true) - $start;
    }

    public static function clone($clonable, $runtime)
    {
        $clone = clone $clonable;
        $clone->runtime = $runtime; // we can access it since we are in Object scope
        return $clone;
    }
}

$objects = [];
while (/*...*/) {
    if (count($objects) > 0) {
        $start = microtime(true);
        if (/*check if would get the same result as the previous one*/) {
            $object = Object::clone(end($objects), microtime(true) - $start);
            $objects[] = $object;
            continue;
        }
    }
    $object = new Object();
    $object->doSome(/*...*/);
    $objects[] = $object;
}

Another option is just to implement setter method for runtime property 另一个选择是仅对runtime属性实施setter方法

Use the magic method: 使用魔术方法:

Once the cloning is complete, if a __clone() method is defined, then the newly created object's __clone() method will be called, to allow any necessary properties that need to be changed. 克隆完成后,如果定义了__clone()方法,则将调用新创建的对象的__clone()方法,以允许需要更改任何必要的属性。

http://php.net/manual/en/language.oop5.cloning.php#object.clone http://php.net/manual/zh/language.oop5.cloning.php#object.clone

class Object
{
    protected $runtime;

    public function __clone() {
        //set $this->runtime
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM