Skip to content

Instantly share code, notes, and snippets.

@comilab
Created September 9, 2013 15:09
Show Gist options
  • Save comilab/6496925 to your computer and use it in GitHub Desktop.
Save comilab/6496925 to your computer and use it in GitHub Desktop.
PHP5.5で追加されたDateTimeImmutableをPHP5.4以下で使う
<?php
class DateTimeImmutable extends DateTime
{
protected static $immutable = true;
public function add($interval)
{
return $this->call('add', func_get_args());
}
public function modify($modify)
{
return $this->call('modify', func_get_args());
}
public function setDate($year, $month, $day)
{
return $this->call('setDate', func_get_args());
}
public function setISODate($year, $week, $day = 1)
{
return $this->call('setISODate', func_get_args());
}
public function setTime($hour, $minute, $second = 0)
{
return $this->call('setTime', func_get_args());
}
public function setTimestamp($unixtimestamp)
{
return $this->call('setTimestamp', func_get_args());
}
public function setTimezone($timezone)
{
return $this->call('setTimezone', func_get_args());
}
public function sub($interval)
{
return $this->call('sub', func_get_args());
}
protected function call($method, array $params)
{
if (self::$immutable) {
self::$immutable = false;
return call_user_func_array(array(clone $this, $method), $params);
} else {
self::$immutable = true;
return call_user_func_array(array('parent', $method), $params);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment