To answer your questions we need to make it clear what they are Traits.
What is a Trait?
Traits are individual pieces of code that define methods that can be used by different classes to provide additional functionality. This is a feature introduced in PHP 5.4.
Traits are very similar to abstract classes, with some differences allowing them to be used by several independent classes at the same time. When I think of traits, I imagine a set of tools like screwdrivers (screwdrivers). If a class wants to use them, just ask.
Source: Imasters
It’s very interesting to think about the example of the hardware box, because that’s the idea. Roughly speaking, you would have with Traits, ways to create a set of functions that can be easily accessed by the classes. You know those functions that you build and end up using in several classes, but then you declare it within each class and consequently end up having a repeat of the code? Your troubles are over, the Traits are here to bring code reuse to your life.
Example:
trait Validacoes {
public function validarCPF($cpf) {
// Aqui vai a lógica de validação e retorna o resultado da mesma
return true;
}
public function validarEmail($email) {
// Aqui vai a lógica de validação e retorna o resultado da mesma
return true;
}
public function validarCNPJ($cnpj) {
// Aqui vai a lógica de validação e retorna o resultado da mesma
return true
}
}
In this trait for example I have ways to perform certain validations. I could use it in my class Usuario
to validate data entries.
class Usuario {
use Validacoes;
private $cpf;
private $email;
private $cnpj;
public function salvar($dados) {
$this->cpf = $this->validarCPF($dados->cpf) ? $dados->cpf : '';
$this->email = $this->validarEmail($dados->email) ? $dados->email : '';
$this->cnpj = $this->validarCNPJ($dados->cnpj) ? $dados->cnpj : '';
}
}
Could still use other traits within my class, just using use Validacoes, Utilidades;
, in case I want to call another Trait called Utilities. It is worth noting that this is one of the advantages of this resource. An extended class has unique inheritance, but using traits, you can use as many as you like, even making it possible to resolve conflicts if different traits have the same function.
Unlike when you simply wear one include
, calls a file with functions and uses them directly within the classes, the traits are compatible with the POO (Object Oriented Programming), this is a great advantage also in your case, because we are talking about a universe MVC.
Answering questions considering the MVC scope:
When thinking about the MVC universe, we could use the traits within a model or controller, depending on the need. In the specific case presented, which is better or more correct, use those functions you created within the trait or extend a model class? As already stated the main goal of the trait is to reuse code, so the answer to your question is simple, if your code is repeated in several classes, be they controller or model, use the trait, will facilitate maintenance.
About performance will simply not affect anything.
Completion
It is recommended to use traits whenever there are functions that are repeated in two or more classes, thus enabling code reuse.
As stated in the PHP manual itself: "A Feature is intended to reduce some limitations of single inheritance by allowing a developer to freely reuse sets of methods in several independent classes residing in different class hierarchies.". Apparently this is not your case. So, no! Do not use Trait.
– Fabiano Monteiro