PHP traits , is there really a reason for that ?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

PHP traits , is there really a reason for that ?

Post by jkon »

I seriously dislike PHP traits ... I can find any other use of it other than “I don't want to write OOP but I pretend I do” am I wrong , is there any real use of it ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP traits , is there really a reason for that ?

Post by Celauran »

I actually find traits to be immensely useful. Think of them as interfaces with implementation. Because PHP only supports single inheritance, traits often allow you to keep your code DRY when disparate classes extending the same parent class just wouldn't make sense.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP traits , is there really a reason for that ?

Post by Christopher »

I agree with Celauran, traits are a DRY way to implement multiple inheritance. They are a structured way to promote code reuse and standardize the implementation common functionality across classes. I'd much rather use traits than resort to very abstract base classes like Object to provide a common interface to many related classes.
(#10850)
Post Reply