2014年12月9日星期二

SitePoint PHP Blog: Using Traits in Doctrine Entities


On the SitePoint PHP blog there's a recent post showing you how to use traits with Doctrine entities. PHP's traits allow for the inclusion of functionality into a class without having to extend another class or create an object to use it.



Since PHP 5.4.0, PHP supports a pretty way to reuse code called "Traits" - a set of methods that you can include within another class in order not to repeat yourself. You can read more about traits in previously published SitePoint posts: here, here and here. Today, I am going to show you how they can be used with Doctrine ORM in a Symfony Environment.


He shows how to create two basic Doctrine entities, in this case representing "Article" and "Comment" instances. He then creates the trait, a "TimestampableTrait" class that abstracts out the setting/updating of the create and updated date on the Doctrine record. He refactors the entities to use the trait and shows the results of the "schema create" command.


Link: http://www.sitepoint.com/using-traits-doctrine-entities/

没有评论:

发表评论