PHPackages                             omaralalwi/php-builders - 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. omaralalwi/php-builders

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

omaralalwi/php-builders
=======================

sample php traits to add ability to use builder design patterns with easy in PHP applications

1.0.1(1y ago)15284↓33.3%11MITPHPPHP ^8.1

Since Jan 15Pushed 1y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (1)Versions (4)Used By (1)

PHP Builders
============

[](#php-builders)

[![Latest Version](https://camo.githubusercontent.com/8ec333e3d5b2a8c936eadd9a27092cf3efc15d705f67c0c7e571d6d8f779e004/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6f6d6172616c616c77692f7068702d6275696c646572732e7376673f7374796c653d666c61742d737175617265)](https://github.com/omaralalwi/php-builders/releases)[![License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://opensource.org/licenses/MIT)

**PHP Builders** is a lightweight PHP library designed to simplify the implementation of the Builder design pattern in PHP applications.

---

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [By Extending `Fluentbuilder` in Builder Class](#by-extending-fluentbuilder-in-builder-class)
    - [By Include Traits in Builder Class](#by-include-traits)
    - [Cast Builder Class](#cast-builder-class)
        - [Return as Array](#return-as-array)
        - [Return as Object](#return-as-object)
        - [Return as JSON](#return-as-json)
- [Custom Execution Logic](#custom-execution-logic)
- [Contributing](#contributing)
- [Security](#security)
- [License](#license)

---

Installation
------------

[](#installation)

Install the package via Composer using the following command:

```
composer require omaralalwi/php-builders
```

---

Usage
-----

[](#usage)

**Note**: if you not use any PHP frameworks like (symfony,laravel,codeigniter,Yii,cakePHP..etc), you should add this line:

```
require_once __DIR__ . '/vendor/autoload.php';
```

**First Way**

### By Extending FluentBuilder in Builder Class

[](#by-extending-fluentbuilder-in-builder-class)

The `FluentBuilder` class integrates all available traits, enabling you to utilize all package features by simply extending this class.

```
namespace App\Builders;

use Omaralalwi\PhpBuilders\FluentBuilder;

class UserBuilder extends FluentBuilder
{
    protected string $name;
    protected string $email;

    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;
        return $this;
    }

    public function sendEmail(): self
    {
        // Logic for sending an email
        return $this;
    }
}
```

**second Way**

### By Include Traits

[](#by-include-traits)

simplify you can achieve same result by include `Buildable`, `Arrayable`, `Objectable`, `Jsonable` traits directly in builder class, without extending the `FluentBuilder` class.

```
namespace App\Builders;

use Omaralalwi\PhpBuilders\Traits\{Buildable, Arrayable, Objectable, Jsonable};
use App\Models\User;

class UserBuilder
{
   use Buildable,
        Arrayable,
        Objectable,
        Jsonable;

        // same code in first way example
}
```

### cast Builder Class

[](#cast-builder-class)

use `UserBuilder` as following:

```
$user = UserBuilder::build()
    ->setName('PHP Builders')
    ->setEmail('hello@phpbuilders.test')
    ->sendEmail();
```

then cast it to needed type as following:

**Return as Array**

Convert the built instance to an array using the `toArray()` method:

```
$userAsArray = $user->toArray();
print_r($userAsArray);
/*
Output:
Array
(
    [name] => PHP Builders
    [email] => hello@phpbuilders.test
)
*/
```

**Return as Object**

Convert the built instance to an object using the `toObject()` method:

```
$userAsObject = $user->toObject();
print_r($userAsObject);
/*
Output:
stdClass Object
(
    [name] => PHP Builders
    [email] => hello@phpbuilders.test
)
*/
```

**Return as JSON**

Convert the built instance to JSON using the `toJson()` method:

```
$userAsJson = $user->toJson();
echo $userAsJson;
/*
Output:
{"name":"PHP Builders","email":"hello@phpbuilders.test"}
*/
```

---

#### Custom Execution Logic

[](#custom-execution-logic)

```
namespace App\Builders;

use Omaralalwi\PhpBuilders\FluentBuilder;
use App\Models\User;

class UserBuilder extends FluentBuilder
{
   // same code in previous example, just we added execute method .

    public function execute(): User
    {
        // Pre-store logic (e.g., validation, preprocessing)
        return $this->store();
    }

    protected function store(): User
    {
        return User::create([
            'name' => $this->name,
            'email' => $this->email,
        ]);
    }
}
```

```
$createdUser = UserBuilder::build()
    ->setName('PHP Builders')
    ->setEmail('hello@phpbuilders.test')
    ->execute();
/*
$createdUser is an instance of the User model.
*/
```

> **Note:** Traits like `Arrayable`, `Objectable`, and `Jsonable` can also be included in your builder class as needed if you choose not to extend `FluentBuilder`.

---

Contributing
------------

[](#contributing)

Contributions are welcome! To propose improvements or report issues, please open an issue or submit a pull request on the [GitHub repository](https://github.com/omaralalwi/php-builders).

---

Security
--------

[](#security)

If you discover any security-related issues, please email the author at: `omaralwi2010@gmail.com`.

---

License
-------

[](#license)

This project is open-source and licensed under the [MIT License](LICENSE).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance41

Moderate activity, may be stable

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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 ~0 days

Total

2

Last Release

487d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/46733da79a34ff686b036a09af1dcc6d21054aee25a5e029500dc87d3c69ba89?d=identicon)[WatheqAlshowaiter](/maintainers/WatheqAlshowaiter)

![](https://www.gravatar.com/avatar/92882431481b621f6509ff259dd600e327c27fc77adcba4fcc7177659059c9a8?d=identicon)[omaralalwi](/maintainers/omaralalwi)

---

Top Contributors

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

---

Tags

builder-patterndesign-patternsfluent-interfaceobject-creationphpphp-buildersphp-codephp-code-generatorphp-libraryphp-patternstraitsphpdesign patternstraitsbuilder-patternfluent-interfaceobject creationomaralalwibuildersphp-buildersphp-patterns

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/omaralalwi-php-builders/health.svg)

```
[![Health](https://phpackages.com/badges/omaralalwi-php-builders/health.svg)](https://phpackages.com/packages/omaralalwi-php-builders)
```

###  Alternatives

[prevailexcel/laravel-action-service-trait

A simple Laravel package to create actions, traits and services using artisan commands

143.0k](/packages/prevailexcel-laravel-action-service-trait)

PHPackages © 2026

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