shawn pyle Mon Nov 07 16:14:34 -0500 2011

Subject: Custom Date class in existing project

I have and existing project that I'm incorporating php-activerecord into. It was created before php 5.3 and has it's own Date class used extensively throughout the code. We want to continue to use our Date class instead of the php-activerecord's for datetime fields pulled from the database. The current solution to this is to create our own Model class with defined __get and __set methods and have our models extend from that. See below.

 1 class Model extends ActiveRecord\Model {
 2     public function x%x%__get($name) {
 3         $value = parent::__get($name);
 4         if($value instanceof DateTime) 
 5             $value = Date::fromDateTime($value->format('Y-m-d H:i:s'));
 6         return $value;
 7     }
 8 
 9     public function __set($name, $value) {
10         if($value instanceof Date) 
11             $value = new DateTime($value->format('Y-m-d H:i:s T'));
12         return parent::__set($name, $value);
13     }

Are there potential problems with this solution apart from the overhead? Is there a better way to do this?