Questions tagged as 'trait'

2
answers

When should I use Inheritance, Abstract Class, Interface or a Trait?

Since PHP 5.4 we have Trait , which "are mechanisms that help (and much) reuse code, and serve perfectly to solve the problem of lack of multiple inheritance." Example of Abastrata Class abstract class AbstractUser { abstract publ...
asked by 08.07.2015 / 18:07
2
answers

Force declaration of properties

I'm implementing a filtering feature in some classes of my application through traits . The function of trait will use class variables through some properties defined in the class: <?php trait FilterTrait { public f...
asked by 29.08.2014 / 21:55
2
answers

How to use Traits in PHP?

I'm creating namespaces for my traits and using them directly, without using them within a specific class, for example: OBS: The code below is just an example. namespace Decrypt; trait Rc4 { public function nome() { retu...
asked by 10.07.2017 / 13:58
1
answer

Differences between traits and namespace for loading

Using namespace loads the file when a method is used say , if it does not invoke any of the class the file will not be loaded. use World; class Hello { World::say() } Using trait even without invoking method or property, the f...
asked by 14.09.2014 / 08:02
1
answer

Why can not a Trait implement an interface?

Why can not a Trait implement an interface in PHP?     
asked by 10.10.2016 / 15:30
1
answer

Traits do not accept overrides of properties?

According to the PHP Manual excerpt    A Trait is intended to reduce some simple inheritance limitations by allowing a developer to freely reuse sets of methods ... See an example: trait Stack { protected $items = []; public fu...
asked by 24.02.2015 / 16:35
1
answer

Why can Traits have directly called methods when they are static?

I just asked a question about Traits and I had another question. If Traits are mechanisms that facilitate the import of methods, because of the limitations of an inheritance in PHP, why can their methods be accessed statically?...
asked by 24.02.2015 / 16:45
2
answers

How to check if a class is using a Trait?

How can we tell 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 = new User; if ($...
asked by 08.07.2015 / 18:13
2
answers

Does using traits replace the role of multiple inheritance?

What happens if I use two different traits in a class, and both have a method with the same name, but different implementations in this method?     
asked by 05.10.2016 / 20:30
1
answer

Use of Trait in static method

Given the example below, how can you use the Trait method inside the static method of the class? Is it possible? How? Is it bad practice? What is the right way? trait TestTraits { public function thistraitmethod($data)...
asked by 18.04.2018 / 15:45