Most voted "trait" questions
Traits are a mechanism for code reuse in simple inheritance languages. They allow to reuse sets of methods freely in various independent classes living in different class hierarchies. Use this tag in conjunction with the corresponding language.
Learn more…17 questions
Sort by count of
-
30
votes2
answers4104
viewsWhen should I use Inheritance, Abstract Class, Interface, or a Trait?
From PHP 5.4 we have the Trait, that "are mechanisms that help (and greatly) the reuse of code, and serve perfectly to solve the problem of lack of multiple inheritance". Example of Abastrata Class…
php interface inheritance abstract-classes traitasked 9 years, 4 months ago Wallace Maxters 102,340 -
18
votes1
answer380
viewsTraits and mixins, what are they? When should I use them? Are there similar mechanisms in other languages?
I was curiously researching about trait here at Sopt and I realized that she is almost always associated with php. But unfortunately the research did not yield me a definitive answer to the concept…
-
10
votes2
answers1084
viewsHow to use Traits in PHP?
I’m creating namespaces for my traits and using them directly, without using them within a specific class, example: NOTE: The code below is just an example. namespace Decrypt; trait Rc4 { public…
-
9
votes2
answers274
views -
7
votes1
answer79
viewsWhy can’t a Trait implement an interface?
Why a Trait cannot implement an interface in PHP?
-
6
votes1
answer296
viewsDifferences between traits and namespace for loading
Using namespace the file is uploaded at the time when some method is used say, If you do not invoke anything from class the file will not be loaded. use World; class Hello { World::say() } Using…
-
5
votes1
answer143
viewsTraits don’t take property superscripts?
According to the excerpt from the PHP Handbook A Trait is intended to reduce some simple inheritance limitations by allowing a developer to reuse sets of methods freely... Take an example: trait…
-
4
votes1
answer65
viewsWhy can traits have methods called directly when they are static?
I just made a question about Traits and another question came to me. If the Traits are mechanisms that facilitate the import of methods, due to the limitations of an inheritance in PHP, because…
-
4
votes2
answers68
viewsHow to check if a class is using a Trait?
How can we know if a class uses a Trait? For example: trait Setter { protected $vars = []; public function __set($key, $value) { $this->vars[$key] = $value; } } class User { use Setter; } $user =…
-
4
votes2
answers1010
viewsDoes the use of traits replace the role of multiple inheritance?
What happens if I use in a class two traits different, and both have a method with the same name, but different implementations in this method?
-
4
votes1
answer56
viewsWhat are the differences between Traits (or Typeclasses) and Interfaces?
Studying Rust, I began to make use of the so-called traits that, according to the language book: We can use traits to define shared behavior in an abstract way. The same chapter further quotes the…
-
2
votes1
answer56
viewsUse of Trait in static method
Taking the example below, how can the Trait within the method static class? Is it possible? How? Is it bad practice? What is the right way? trait TestTraits { public function thistraitmethod($data)…
-
2
votes0
answers45
viewsWhy is my localhost adding an extra bar?
trait Pagination{ private $totalRegistros; private $limite; private $offset; private $totalPaginas; private $segmentoUrl; private $links; private $explodeBarrasUrl; private $posicaoPaginaUrl; static…
-
2
votes1
answer286
viewsDoes typescript support an implementation equivalent to trait?
I am developing a project using Ionic, and would like to better organize my classes. Below is an example of how I would do with PHP: <?php trait comportamento { public function ficarTriste() { }…
-
1
votes0
answers305
viewsError when instantiating object inside a namespace
I’m trying to use the mpdf library to print in PDF. However, the following error is occurring when instantiating the library: Fatal error: Trait 'Mpdf\Strict' not found in…
-
1
votes0
answers27
viewsDoubt Laravel Trait
Good staff , I have the following Trait in the Laravel <?php namespace App\Traits; trait Imageable { public function getUrlImageAttribute(): string { .... } } I use the same in the following…
-
0
votes0
answers43
viewsPhpunit call native function once and return stdClass
github.com/Hubtodo/traits/blob/master/tests/Jsonrendertest. p hp I’m doing a successful test of my trait function My trait is this: <?php namespace HubToDo\Traits; use InvalidArgumentException;…