PHP Classes

What are the benefits?

Recommend this page to a friend!

      PHP Classes blog  >  How to Make Better Re...  >  All threads  >  What are the benefits?  >  (Un) Subscribe thread alerts  
Subject:What are the benefits?
Summary:Understand how, but not why.
Author:Hugo Wijdeveld
Date:2015-08-13 12:00:46
Update:2015-08-14 00:50:27

  1. What are the benefits?   Reply   Report abuse  
Picture of Hugo Wijdeveld Hugo Wijdeveld - 2015-08-13 15:48:17
Hi Dave, I have a really hard time figuring out the use of traits. The only thing I can come up with is that some code just doesn't fit into classes. I have those in one "" sheet. This works (these functions are instantly available to all classes), but it's pretty procedural style and harder to maintain than oop. So my question to you would be: is that the benefit we can expect from traits or is there anything more?

  2. Re: What are the benefits?   Reply   Report abuse  
Picture of Dave Smith Dave Smith - 2015-08-14 00:50:27 - In reply to message 1 from Hugo Wijdeveld
Multiple inheritance languages allow a class to be extended by multiple classes, however PHP is a single inheritance language, so a class is only extended by one additional class. We can chain the extensions so that class1 is extended by class2 which is extended by class3 and so on. We can't extend class1 with class2 and class3 at the same time.

The primary use of traits is to provide a means for multiple inheritance within a single inheritance language, like PHP.

If I am a lead developer on a project, I can easily set up what each class will do and have a set of common named functions defined for each script to have global access to. Any other developers working under my lead would simply include the common functions and use them, the entire project could even be procedural if that is how I have designed it.

What happens if my team has only a limited control over part of a project, or my piece of code is going to be used in multiple projects that I have no control over? I can use class abstraction or even better, since PHP 5.4 I can use traits which have all the advantages of abstraction with the added benefit of conflict resolution.

All of this will be discussed in greater detail in the next part of this article.