What is the structure of the signs of Laravel?

When looking at the source of Laravel, I noticed a lot of these things:

Controller Class:

class Controller extends BaseController { use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests; } 

One of its component features:

 trait AuthorizesRequests { /** * Authorize a given action against a set of arguments. * * @param mixed $ability * @param mixed|array $arguments * * @return \Illuminate\Auth\Access\Response * * @throws \Illuminate\Auth\Access\AuthorizationException */ public function authorize($ability, $arguments = []) { list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments); return app(Gate::class)->authorize($ability, $arguments); } // ... } 

I have a few questions about this:

  • Does this template (abstracting reuse functionality in features) have a name?
  • Is this template useful for any other projects?
  • If a trait requires dependencies, is there a better way to implement them, instead of using a service locator (e.g. app() ) in this case?

I am considering using this approach in my code to share some common functions between several of my classes. I am considering the possibility of creating a ChecksBarcodes attribute that will work with the inventory information store and share this attribute between several similar but unrelated product management classes that everyone should check for barcodes.

+5
source share
2 answers

Features introduced with PHP 5.4 are a general solution to one big PHP problem: single inheritance. A rough assumption from me: if PHP supports multiple inheritance (inheriting from more than one class), there would be no sign.

However, traits are a good thing to reduce code duplication and, in addition, ensure the same functionality for multiple classes.

  • As far as I can tell, for the use of signs there is no real (boilerplate) name.
  • This is not a model as such, compared to another software design pattern, just call it traits;)
  • Laravel, and perhaps more specifically the Cashier package, are good examples of using traits. If someone finds other good examples, please write about it.
  • Features may be expanded with other features. This, of course, creates increasing complexity. For expansion, you should probably consider other methods to provide functionality for your class. A β€œchain” of attributes adds a load of complexity.
+1
source

Features similar to expanding classes, but with a few differences

  • Traits do not have a constructor
  • Classes can propagate only one class, but have several attributes

They are similar to mixins in other languages. I think you could say that this is an easy way to use the DRY principle.

Since traits do not have constructors, any dependencies that they have must exist in the class in which they are used. I think, depending on the class, that something different from the trait would be a bad design template. So you have to use the service locator to freeze.

If you prefer not to use a service locator, I would recommend using a class rather than a dash. You may have a BarcodeChecker class that you can insert into the constructor of the classes you want to use. Then instead of $this->checkBarcode() you should use $this->barcodeChecker->check() . I think this will be the best design pattern if dependencies are needed for this property.

+2
source

All Articles