https://github.com/nabeghe/traituctor
Invoking a pseudo constructor for each trait from within the main constructor.
https://github.com/nabeghe/traituctor
constructor php php-library php-traits php8 phplib phplibrary phptrait reflection trait
Last synced: 6 months ago
JSON representation
Invoking a pseudo constructor for each trait from within the main constructor.
- Host: GitHub
- URL: https://github.com/nabeghe/traituctor
- Owner: nabeghe
- License: mit
- Created: 2024-10-18T17:28:36.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2024-11-10T01:43:14.000Z (11 months ago)
- Last Synced: 2025-03-29T02:21:43.117Z (6 months ago)
- Topics: constructor, php, php-library, php-traits, php8, phplib, phplibrary, phptrait, reflection, trait
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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:
```bash
composer require nabeghe/traituctor
```
### Examples
#### Example - No Requirments:
```php
use Nabeghe\Traituctor\Traituctor;trait A
{
protected $numberA;public function __constructA($baseNumber)
{
echo "A\n";
$this->numberA = $baseNumber + 3;
}
}trait B
{
protected $numberB;public function __constructB($baseNumber)
{
echo "B\n";
$this->numberB = $baseNumber + 4;
}
}class Main
{
use A, B;public function __construct($baseNumber)
{
echo "Main\n";
Traituctor::construct($this, [$baseNumber]);
}public function multiply()
{
return $this->numberA * $this->numberB;
}
}echo (new Main(10))->multiply();
// Main
// A
// B
// 182
```#### Example - Requirments:
**Notice:** Supported only in PHP 8 or higher.
```php
use Nabeghe\Traituctor\Traituctor;
use Nabeghe\Traituctor\Requirements;#[Requirements(B::class)]
trait A
{
protected $numberA;public function __constructA($baseNumber)
{
echo "A\n";
$this->numberA = $this->numberB + 1;
}
}trait B
{
protected $numberB;public function __constructB($baseNumber)
{
echo "B\n";
$this->numberB = $baseNumber + 3;
}
}class Main
{
use A, B;public function __construct($baseNumber)
{
echo "Main\n";
Traituctor::construct($this, [$baseNumber], true);
}public function multiply()
{
return $this->numberA * $this->numberB;
}
}echo (new Main(10))->multiply();
// Main
// B
// A
// 182
```
## ๐ License
Copyright (c) 2024 Hadi Akbarzadeh
Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.