Download the PHP package nabeghe/traituctor without Composer
On this page you can find all versions of the php package nabeghe/traituctor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nabeghe/traituctor
More information about nabeghe/traituctor
Files in nabeghe/traituctor
Package traituctor
Short Description Invoking a pseudo constructor for each trait from within the main constructor.
License MIT
Homepage https://github.com/nabeghe/traituctor
Informations about the package traituctor
Traituctor (pseudo-constructor for php traits)
Invoking a pseudo constructor for each trait from within the main constructor.
Imagine a class uses multiple traits, where each trait requires a method to be executed during the class's instantiation for initialization purposes. Since in PHP only one trait added to a class can have a constructor, and others cannot, one possible solution is to define initializtion methods in each trait and manually call them within the main class constructor. Another approach would be to check inside each trait’s method whether it has been initialized before executing it, and then initialize it if necessary.
However, the current library handles this process automatically. Here, you have a pseudo-constructor for each trait, and by invoking a method within the main class constructor, all of them are executed in sequence. Moreover, you can control the execution order by using an attribute called 'Requirements'. This attribute allows you to define the dependencies between traits, ensuring that the pseudo-constructor of a required trait is executed before its dependent trait. Alternatively, you could omit this attribute and simply use the traits in the desired order in the main class, but the attribute guarantees the correct sequence.
🫡 Usage
🚀 Installation
You can install the package via composer:
Examples
Example - No Requirments:
Example - Requirments:
Notice: Supported only in PHP 8 or higher.
📖 License
Copyright (c) 2024 Hadi Akbarzadeh
Licensed under the MIT license, see LICENSE.md for details.