克隆实例并链接方法调用,可能吗?
是否可以通过链接来<code>克隆一个调用其方法的实例?这给了我一个语法错误:
/**
* Parse an object containing (eventually) "duration" property or "year" (and
* eventually) "month" properties.
*
* @return array Array containing start date and end date DateTime objects.
*/
public function parseTimeArgs($args)
{
$now = new DateTime();
if(isset($args->duration) && $duration = new DateInterval($args->duration))
return array((clone $now)->sub($duration), $now);
}
Is possible to clone
an instance calling a method on it with chaining? This gives me a syntax error:
/**
* Parse an object containing (eventually) "duration" property or "year" (and
* eventually) "month" properties.
*
* @return array Array containing start date and end date DateTime objects.
*/
public function parseTimeArgs($args)
{
$now = new DateTime();
if(isset($args->duration) && $duration = new DateInterval($args->duration))
return array((clone $now)->sub($duration), $now);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。您可以使用“工厂”方法来代替:
旁注:目前也无法通过这种方式使用
new
运算符。在即将发布的 PHP 5.4 版本中,new
可以实现此功能,如下所示:但此处不支持克隆。
No, this is not possible. You could use a "factory" method instead:
Side note: Using the
new
operator is currently not possible this way either. In the upcoming PHP 5.4 release this will be possible fornew
as follows:Clone however is not supported here.