PHPackages                             phputil/traits - 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. phputil/traits

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

phputil/traits
==============

Useful traits for PHP

1.3(9y ago)81.4k↓50%1LGPL-3PHPPHP &gt;=5.4

Since Aug 16Pushed 6y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (5)Used By (1)

phputil\\traits
===============

[](#phputiltraits)

Useful traits for PHP.

[![Build Status](https://camo.githubusercontent.com/c1ae3c76939d3e12df78b9f4f11274e902f3b4d064f003067436f78cce26fc8c/68747470733a2f2f7472617669732d63692e6f72672f74686961676f64702f7472616974732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/thiagodp/traits)

We use [semantic version](http://semver.org/). See [our releases](https://github.com/thiagodp/traits/releases).

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

[](#installation)

```
composer require phputil/traits
```

Traits
------

[](#traits)

- [GetterBuilder](https://github.com/thiagodp/traits/blob/master/lib/GetterBuilder.php)
- [WithBuilder](https://github.com/thiagodp/traits/blob/master/lib/WithBuilder.php)
- [GetterSetterWithBuilder](https://github.com/thiagodp/traits/blob/master/lib/GetterSetterWithBuilder.php)
- [FromArray](https://github.com/thiagodp/traits/blob/master/lib/FromArray.php)
- [ToArray](https://github.com/thiagodp/traits/blob/master/lib/ToArray.php)

Examples
--------

[](#examples)

Example on `GetterBuilder`:

```
use phputil\traits\GetterBuilder;

class MyClass {

	use GetterBuilder; // simulate getters

	private $name = '';
	private $description = '';

	function __construct( $name, $description ) {
		$this->name = $name;
		$this->description = $description;
	}
}

$obj = new MyClass( 'Bob', 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob
```

Example on `WithBuilder`:

```
use phputil\traits\WithBuilder;

class MyClass {

	use WithBuilder;

	public $name = '';
	public $description = '';
}

$obj = ( new MyClass() )->withName( 'Bob' )->withDescription( 'I am Bob' );
echo $obj->name; // Bob
echo $obj->description; // I am Bob
```

Example on `GetterSetterWithBuilder`:

```
use phputil\traits\GetterSetterWithBuilder;

class MyClass {

	use GetterSetterWithBuilder;

	private $name = '';
	private $description = '';
}

$obj = ( new MyClass() )->withName( 'Bob' )->setDescription( 'I am Bob' );
echo $obj->getName(); // Bob
echo $obj->getDescription(); // I am Bob
$obj->setName( 'Bob Dylan' );
echo $obj->getName(); // Bob Dylan
```

Example on `FromArray`:

```
use phputil\traits\FromArray;

class MyClass {

	use FromArray;

	private $id;
	protected $name;
	public $age;
}

$obj = new MyClass();
$obj->fromArray( array( 'id' => 10, 'name' => 'Bob', 'age' => 18 ) );
var_dump( $obj ); // the attributes will have the array values
```

Example on converting from a dynamic object:

```
// From a converting from a dynamic object, just use a type casting
$p = new \stdClass;
$p->id = 10;
$p->name = 'Bob';
$p->age = 18;

$obj = new MyClass();
$obj->fromArray( (array) $p ); // Just make a type casting to array ;)
```

Example on `ToArray`:

```
use phputil\traits\ToArray;

class MyClass {

	use ToArray;

	private $id = 50;
	protected $name = 'Bob';
	public $age = 21;
}

$obj = new MyClass();
var_dump( $obj->toArray() ); // array( 'id' => 50, 'name' => 'Bob', 'age' => 21 )
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity61

Established project with proven stability

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

Total

4

Last Release

3421d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/154324d722a6ee9c252a0338329781084a97af2d0ea9faaf39176df5a689a2ec?d=identicon)[thiagodp](/maintainers/thiagodp)

---

Top Contributors

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

---

Tags

arraybuildergeneratorgetterphpphputilsettertraittraitbuildergettersetterautomatic

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phputil-traits/health.svg)

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

###  Alternatives

[usmanhalalit/get-set-go

Dynamic Setter-Getter for PHP 5.4+

1813.4k1](/packages/usmanhalalit-get-set-go)[antares/accessible

PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.

123.9k1](/packages/antares-accessible)

PHPackages © 2026

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