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);
}
No, this is not possible. You could use a “factory” method instead:
Side note: Using the
newoperator is currently not possible this way either. In the upcoming PHP 5.4 release this will be possible fornewas follows:Clone however is not supported here.