PHPackages                             nabeghe/traituctor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nabeghe/traituctor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

nabeghe/traituctor
==================

Invoking a pseudo constructor for each trait from within the main constructor.

0.1.1(1y ago)43MITPHPPHP &gt;=7.4

Since Oct 18Pushed 10mo ago1 watchersCompare

[ Source](https://github.com/nabeghe/traituctor)[ Packagist](https://packagist.org/packages/nabeghe/traituctor)[ Docs](https://github.com/nabeghe/traituctor)[ RSS](/packages/nabeghe-traituctor/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Traituctor: Traits Pseudo-Constructor for PHP ≥ 7.4
===================================================

[](#traituctor-traits-pseudo-constructor-for-php--74)

> 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
-------

[](#-usage)

### 🚀 Installation

[](#-installation)

You can install the package via composer:

```
composer require nabeghe/traituctor
```

### Examples

[](#examples)

#### Example - No Requirments:

[](#example---no-requirments)

```
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:

[](#example---requirments)

**Notice:** Supported only in PHP 8 or higher.

```
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
---------

[](#-license)

Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance47

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity30

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~22 days

Total

2

Last Release

555d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dae1bf378bcf40fe55c193160377d613d628188587554a3fb11cdec6f0521ba1?d=identicon)[nabeghe](/maintainers/nabeghe)

---

Top Contributors

[![nabeghe](https://avatars.githubusercontent.com/u/12207627?v=4)](https://github.com/nabeghe "nabeghe (4 commits)")

---

Tags

constructorphpphp-libraryphp-traitsphp8phplibphplibraryphptraitreflectiontraithelperconstructorconstructreflectionlibrarytraitsupporttrait constructortrait helperpseudo constructor

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nabeghe-traituctor/health.svg)

```
[![Health](https://phpackages.com/badges/nabeghe-traituctor/health.svg)](https://phpackages.com/packages/nabeghe-traituctor)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
