【发布时间】:2018-08-08 09:45:49
【问题描述】:
我遇到了一个逻辑问题。
我有一个需要克隆的对象。
对象是|具有计算结果。
对象有一个运行时。
在某些情况下,克隆对象比再次计算结果更快
(f.e. 相同的参数 ^= 相同的结果).
但必须不复制运行时。
运行时将是确定我可以使用相同结果(对象)的时间。
例子:
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;
}
我如何克隆前一个对象并在克隆上设置实际运行时而不使运行时属性公开?
【问题讨论】:
-
请不要将解决方案添加到您的问题中。相反,您可以通过单击复选标记来选择下面的答案,或者自己回答问题。您的问题已回滚到以前的状态。欲了解更多信息,请访问help center。
标签: php object properties clone