PHPackages                             itrocks/build - 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. itrocks/build

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

itrocks/build
=============

Extend your PHP classes: add interface and traits or replace with a child class

00PHP

Since May 17Pushed 3y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

PHP class builder
-----------------

[](#php-class-builder)

This it.rocks library allows you to statically extend your PHP classes: add interface and traits or replace them with a child class when they are instantiated. This is not simply mocking, its aim is to allow you to create highly configurable applications, with class features activated or not, depending on the deployment environment.

It is designed to be simple to apply to any PHP project, and fast to execute.

With it, you don't have to prepare your code for dependency injection. The goal is to keep your coded applications as simple as possible, using any way php works, without embedding the complexity of dependency injection design pattern implementations.

Limitations: this is about extending base class capabilities, not totally make dependency implementations uncoupled. This tool is here to apply the Open/closed principle (the O from [SOLID](https://en.wikipedia.org/wiki/SOLID)) to projects that need to be modular and deployed with or without some modules.

Pre-requisites
--------------

[](#pre-requisites)

- This works with PHP 8.2 only. I wanted to make full use of the current PHP version features.
    To install php 8.2 on a Debian/Ubuntu/Mint system: .

Development progress
--------------------

[](#development-progress)

This library is currently on a development phase. It will follow the it.rocks [Builder](https://github.com/itrocks/framework/tree/master/builder) module specifications to generalize the way it works:

- to be used in any PHP project, even if non-it.rocks,
- to be applied to vendor libraries.

Nothing work today, but it is coming soon.

How it works
------------

[](#how-it-works)

- You write the class building table into a configuration array: class =&gt; child class or class =&gt; added interfaces and traits.
- Your traits can embed rules about inheritability: if you need some traits to be on another class level than others, because they extend their capabilities, you will use the #Extend attribute to describe this hierarchy rule.
- Your project must have an "update" phase: a procedure that will run everytime a php file is modified.
- During this phase, the builder uses itrocks/class-use to scan all class dependencies.
- Using the class replacement table, the builder prepares the dynamic implementation of built replacement classes.
- Everywhere your application or vendors use a class, the class name is replaced by the built class reference. The modification is directly done into the tokens tree and the files marked for writing.
- The modified PHP files are written the cache directory.
- To use the modified PHP files instead of the original, a PHP file input filter is applied. It allows you to debug your applications and get error messages referring to the original PHP file, but the actual executed code comes from the cached modified PHP scripts.

This make it very fast on execution time: when you run your scripts, no more code than necessary is executed to instantiate your built classes instead of the original ones. You can use PHP oriented object programming the simplest way: without interface when you do not need it, and you can inject dependencies into any exiting class or trait using this library, without the original code being prepared for this.

Example
-------

[](#example)

Considering this class:

```
class Car
{
  int $engine_power = 100;

  function getMaxSpeed() : int
  {
    return round($this->engine_power * 1.5);
  }
}
```

You created another module, optional on your project deployment phase, to manage the weight of the cas and its effect on its max speed:

```
#[Extend(Car::class)]
trait Has_Weight
{
  int $weight = 500;

  function getMaxSpeed() : int
  {
    return round(parent::getMaxSpeed() / ($this->weight / 400));
  }
}
```

One of your deployment configuration files describes the way you "improve" your car, only for your final applications that need this added code:

```
$config['build'] = [
  Car::class => [Has_Weight::class]
];
```

Then, when your source code simply instantiate a car and gets its maximum speed information, you don't have to change anything: itrocks/build will make the replacement into an internal cache file and instantiate the new replacement class:

```
$car = new Car;
echo "My car maximum speed is " . $car->getMaxSpeed() . "\n";
```

The code that will be really executed will be the creation of a new replacement class, and the replacement of all references to the original class name by the new one:

The internally added code:

```
class B\Car { use Has_Weight; }
```

The internally replaced code for your program, which will be executed:

```
$car = new B\Car;
echo "My Car maximum speed is " . $car->getMaxSpeed() . "\n";
```

Known potential issues
----------------------

[](#known-potential-issues)

- Missing re-building a disappeared cache file (configuration unchanged).
- Missing re-building configurations for which any of the source or composition files changed.
- Missing purging cache files when stopping building a class/trait.

Known limits
------------

[](#known-limits)

These use cases are known for non-working today. May be managed later:

- Building a trait using additional traits which one extends another one will not work as expected: will crash if there is a any methods conflict. Extension traits with extends will work only for classes.

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity22

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c59494f987e0e7b01198896224a0ed287c0406c2d216b9c6d7a239d912fb7c5?d=identicon)[itrocks](/maintainers/itrocks)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/itrocks-build/health.svg)

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

###  Alternatives

[aimeos/aimeos-base

Aimeos base layer for abstracting from host environments

2.1k134.0k1](/packages/aimeos-aimeos-base)[microweber/screen

A PHP Class to interact with PhantomJs and capture screenshot of a webpage

632226.0k1](/packages/microweber-screen)[fuel/docs

FuelPHP 1.x Documenataion

166542.3k4](/packages/fuel-docs)[ec-europa/toolkit

Toolkit packaged for Drupal projects based on Robo.

38244.6k16](/packages/ec-europa-toolkit)[fof/user-directory

The permission based public user directory extension for your Flarum forum.

2789.7k5](/packages/fof-user-directory)[orangesoft/throttler

Load balancer between nodes.

1732.3k1](/packages/orangesoft-throttler)

PHPackages © 2026

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