我正在寻找一种扩展PHP类以添加自定义方法的方法。特别是我想为MongoDate添加一个日期格式方法(来自PHP MongoDB驱动程序)。PHP - 向类中添加方法
我只是认为它会更清洁,如果从Mongo收集中收到的MongoDate对象提供了一种方法来使其可读,而不需要调用函数或类来做到这一点。
$d = new MongoDate();
some_date_format($d); // the way it works now
$d->format(); // the way it would be cleaner
有没有解决方法?
阅读[php object inheritance](http://php.net/manual/en/language.oop5.inheritance.php):-) – 2012-03-01 21:20:52
同样在这里,我想从MongoCollection :: find ();'也会受到影响。如果我只是将从find中得到的MongoDate投给我的新类,它是否工作? – 2012-03-01 21:22:48
这里的适配器模式可能更合适。检查出来:http://c2.com/cgi/wiki?AdapterPattern – MitMaro 2012-03-01 21:24:02