PHPackages                             bapcat/propifier - 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. bapcat/propifier

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

bapcat/propifier
================

A trait that improves PHP's properties

3.1.1(9mo ago)12.6k[1 issues](https://github.com/BapCat/Propifier/issues)7GPL-3.0-or-laterPHPPHP ^8.1CI failing

Since Aug 11Pushed 9mo ago3 watchersCompare

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

READMEChangelog (8)Dependencies (3)Versions (9)Used By (7)

[![Build Status](https://camo.githubusercontent.com/6b8c689aeb8c1a639a75c0a6b4b30a77db739fd234fb677152850ca762f07083/68747470733a2f2f7472617669732d63692e6f72672f4261704361742f50726f7069666965722e7376673f6272616e63683d312e322e30)](https://travis-ci.org/BapCat/Propifier)[![Coverage Status](https://camo.githubusercontent.com/5699b52a30ef3ca2825762f94784baaa0825f599be90c58c64434a00903cdb1c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f4261704361742f50726f7069666965722f62616467652e7376673f6272616e63683d312e322e30)](https://coveralls.io/r/BapCat/Propifier?branch=1.2.0)[![License](https://camo.githubusercontent.com/9bfe51e9301dfa5d68b7d9f7a1d9d9ab171dccd49955c98607f55ef26ec22b95/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f4261704361742f50726f7069666965722e737667)](https://img.shields.io/packagist/l/BapCat/Propifier.svg)

Propifier
=========

[](#propifier)

A trait that adds real object-oriented property support to PHP.

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

[](#installation)

### Composer

[](#composer)

[Composer](https://getcomposer.org/) is the recommended method of installation for all BapCat packages.

```
$ composer require bapcat/propifier

```

### GitHub

[](#github)

BapCat packages may be downloaded from [GitHub](https://github.com/BapCat/Propifier/).

The Problem With PHP "Properties"
---------------------------------

[](#the-problem-with-php-properties)

Anyone coming from another language like .NET will probably be accustomed to defining properties (accessors and mutators) to control access to private variables of a class. Unfortunately, PHP lacks support for this useful feature. There are several workarounds...

#### Public Properties

[](#public-properties)

```
class Foo {
  public $a;
  public $b;
}
```

Using public properties is the easiest, but completely lacks encapsulation. You have no control over who can change the internal state of your object.

### `__get` and `__set`

[](#__get-and-__set)

```
class Foo {
  private $a;
  private $b;

  public function __get($name) {
    if(isset($this->$name)) {
      return $this->$name;
    }

    throw new Exception('Invalid property!');
  }

  public function __set($name, $value) {
    switch($name) {
      case 'a':
        $this->a = $value;
      break;

      case 'b':
        throw new Exception('b is read-only!');
    }

    throw new Exception('Invalid property!');
  }
}
```

Using PHP's late binding support gives you control over what can be read from and written to your object, but sacrifices readability, efficiency, and type hinting. It's also not possible to control read/write access to arrays this way without using other workarounds.

```
...
  private $array = [];
...
  public function __get($name) {
    if(isset($this->$name)) {
      return $this->$name;
    }
  }

  public function __set($name, $value) {
    throw new Exception('You can\'t set me!');
  }
...

$foo = new FooWithArrayThatCantBeSet();

$foo->array['a'] = 'Test'; // Note: no exception
echo $foo->array['a']; // -> 'Test'
```

#### Getters and Setters

[](#getters-and-setters)

```
class Foo {
  private $a;
  private $b;
  private $array = [];

  public function getA() {
    return $this->a;
  }

  public function setA(A $a) {
    $this->a = $a;
  }

  public function getB() {
    return $this->b;
  }

  public function getOnlyOneArrayValue($index) {
    return $this->array[$index];
  }
}
```

Using Java-style getters and setters is one of the best ways to implement properties in PHP, but still has flaws. It is very verbose:

```
$a = $foo->getA(); // rather than $foo->a
```

You must also forgo using array access syntax to access array properties:

```
$one_array_value = $foo->getOnlyOneArrayValue(1); // rather than $foo->array[1]
```

The Propifier Way
-----------------

[](#the-propifier-way)

Propifier solves every one of these problems.

```
class Foo {
  use \BapCat\Propifier\PropifierTrait;

  private $a;
  private $b;
  private $array = [];

  public function __construct() {
    $a = null;
    $b = new B();
    $array['test'] = 'Test';
  }

  protected function getA() {
    return $this->a;
  }

  protected function setA(A $a) { // Type hinting
    $this->a = $a;
  }

  protected function getB() {
    return $this->b;
  }

  // Controlled access
  //protected function setB(B $b) {
  //  $this->b = $b;
  //}

  // Propifier automatically detects arrays, and
  // allows array access when using the property
  protected function getArray($index) {
    return $this->array[$index];
  }

  // You can even define iterators to add foreach support
  protected function itrArray() {
    return new ArrayIterator($this->array);
  }
}
```

```
$foo = new Foo();

echo $foo->a; // -> null
$foo->a = new A(); // $a == new instance of A

echo $foo->b; // -> instance of B
$foo->b = new B(); // exception

echo $foo->array['test']; // -> 'Test'
$foo->array = []; // exception
$foo->array[1] = 'Test?'; // exception

foreach($foo->array as $key => $value) {
  // ...
}
```

#### Efficiency

[](#efficiency)

Propifier will make you more efficient at writing code that matters, and unlike similar solutions, Propifier is designed from the ground up to be fast. It figures everything out at the start, and maintains a static mapping of all of your objects' properties so using them is always fast.

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% 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 ~521 days

Recently: every ~854 days

Total

8

Last Release

279d ago

Major Versions

1.2.0 → 2.02019-01-03

2.0 → 3.02021-02-18

PHP version history (4 changes)1.0.0PHP &gt;=5.5.0

2.0PHP ^7.1

3.0PHP ^7.4|^8.0

3.1PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/246ea082e8ed7f6e8ace29950179d4dfb1e3a58bac53aef49a3d2e43ab1e7f04?d=identicon)[LordMonoxide](/maintainers/LordMonoxide)

---

Top Contributors

[![LordMonoxide](https://avatars.githubusercontent.com/u/437657?v=4)](https://github.com/LordMonoxide "LordMonoxide (38 commits)")[![rascopeeko](https://avatars.githubusercontent.com/u/6076349?v=4)](https://github.com/rascopeeko "rascopeeko (1 commits)")

---

Tags

setpropertypropertiesaccessormutatorgetgettersetter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bapcat-propifier/health.svg)

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

###  Alternatives

[antares/accessible

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

123.9k1](/packages/antares-accessible)[antares/accessible-bundle

A bundle to easily use Accessible in Symfony projects.

153.8k](/packages/antares-accessible-bundle)

PHPackages © 2026

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