0
votes

Let's say

a) I have trait and use it in another class. I know how that works.

b) I have normal random class called Helper and I have functions in that Helper class and I include that class in another class. I can use those helper class's functions in another class by using

Helper::method()

if method is static or use new keyword and use the method like

$helper->method().

c) I can have just a PHP file and functions in it, no class at all. I can include it in another class and just use it just this way:

method()

d) I can have a class, and then another class can extend it and have its functions, but this way is bad, as I can't extend two classes at the same time.

Why is using trait the best way compared to b) and c) solutions?

1
Define "best". Is it "best"? Depends on the requirements.deceze

1 Answers

-1
votes

Why is using trait the best way compared to b) and c) solutions ?

Each of your examples are a good solution for different developments.

 Helper::method();
 Helper->method();

You have a function you will use in many Classes. The function is relatively generic. Then put it on a Helper class, and if possible, set it static

 method();

That's another question: procedural or oriented object development ? Then it depends on the context.

Trait{ ... }

Some specific (or not) functions which need to be present on different classes ? Or you want to sort your functions by their use, or something else (I do that a lot when I have too many functions in one class)


But remember that there are some conventions to follow when you develop. If you want to share your code (GitHub for example), it's better to stick to them (not really an obligation but well...).

To conclude, it depend on whant you want to do, on the context of your development. There is no method better than the others.