PHPackages                             serafim/properties - 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. serafim/properties

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

serafim/properties
==================

PHP properties implementation

2.0.1(7y ago)779.1k4[2 issues](https://github.com/SerafimArts/Properties/issues)1MITPHPPHP ^7.1.3

Since Apr 14Pushed 7y ago7 watchersCompare

[ Source](https://github.com/SerafimArts/Properties)[ Packagist](https://packagist.org/packages/serafim/properties)[ Docs](http://github.io/SerafimArts/Properties)[ RSS](/packages/serafim-properties/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (5)Dependencies (9)Versions (6)Used By (1)

PHP Properties
==============

[](#php-properties)

 [![Travis CI](https://camo.githubusercontent.com/045f44d7766c60c36487908cda21f5a8ab015c246419916b7c9bffe23f693870/68747470733a2f2f7472617669732d63692e6f72672f5365726166696d417274732f50726f706572746965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/SerafimArts/Properties) [![Scrutinizer CI](https://camo.githubusercontent.com/4b9e6f03252a97bf2f5a24c24c2d7c4f17d504f442fa6ac016700fe079738309/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5365726166696d417274732f50726f706572746965732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/SerafimArts/Properties/?branch=master) [![Latest Stable Version](https://camo.githubusercontent.com/17e105830be9fc165c7bb18a50ca3145ecfaee1cce3ca5f458bf73582ac5ce5d/68747470733a2f2f706f7365722e707567782e6f72672f7365726166696d2f70726f706572746965732f76657273696f6e)](https://packagist.org/packages/serafim/properties) [![Latest Unstable Version](https://camo.githubusercontent.com/ede25826721b373d4ee1b106fea5c7a77af126189a1fcbaac88df2b6959f11cc/68747470733a2f2f706f7365722e707567782e6f72672f7365726166696d2f70726f706572746965732f762f756e737461626c65)](https://packagist.org/packages/serafim/properties) [![License MIT](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://raw.githubusercontent.com/serafim/properties/master/LICENSE.md) [![Total Downloads](https://camo.githubusercontent.com/355d2b8f5627659733ff85c7d2afcbd5cfc77c33f8330d25372c3eac6e8f6d58/68747470733a2f2f706f7365722e707567782e6f72672f7365726166696d2f70726f706572746965732f646f776e6c6f616473)](https://packagist.org/packages/serafim/properties)

PHP Properties implementations based on getters or setters method and used [PSR-5 PHPDoc](https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md) information.

- [Installation](#installation)
- [Introduction](#introduction)
- [Properties Usage](#properties-usage)
    - [Readonly Properties](#readonly-properties)
    - [Writeonly Properties](#writeonly-properties)
    - [Getters And Setters](#getters-and-setters)
    - [Autocomplete](#autocomplete)
- [Type Hints](#type-hints)
    - [Primitive Type Hints](#primitive-type-hints)
    - [Arrays And Generics](#arrays-and-generics)
    - [Conjunction And Disjunction](#conjunction-and-disjunction)
- [Production Mode](#production-mode)

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

[](#installation)

`composer require serafim/properties`

[Package on packagist.org](https://packagist.org/packages/serafim/properties)

Introduction
------------

[](#introduction)

Properties provide the ability to control the behavior of setting and retrieving data from an object fields.

There are **no properties** at the **language level**, but they can be implemented with the some additional functionality.

For example:

```
class MyClass
{
    protected $a = 23;

    public function __get($field)
    {
        return $this->$field;
    }
}

$dto = new MyClass();
echo $dto->a; // 23
$dto->a = 42; // Cannot access protected property MyClass::$a
```

This code example does not provide type-hint and autocomplete. Using magic without the ability to control it is always a bad option. ...well, it looks awful %)

We can fix some problems using PSR-5. In this case, we can add a docblock.

```
/**
 * @property-read int $a
 */
class MyClass
{
    // ...
```

But this docblock only adds information for the IDE and does not affect the code itself. At the same time, you should always keep it up to date.

But wait! We have another problem.

```
$dto = new MyClass();
echo isset($dto->a); // false
```

This is obviously a bug. We still need to add `__isset`, `__unset` and `__set` support.

It's damn awful!!111

Properties Usage
----------------

[](#properties-usage)

Now let's see what the `serafim/properties` package provides.

```
/**
 * @property $a
 */
class MyClass
{
    use Serafim\Properties\Properties;

    protected $a = 23;
}

$dto = new MyClass();
$dto->a;      // 23
$dto->a = 42; // Ok
$dto->a;      // 42
```

### Readonly Properties

[](#readonly-properties)

To indicate that the type should be readonly use `@property-read` annotation.

```
/**
 * @property-read $a
 */
class MyClass
{
    use Serafim\Properties\Properties;

    protected $a = 23;
}

$dto = new MyClass();
$dto->a;      // 23
$dto->a = 42; // Error: Property MyClass::$a is readonly
```

### Writeonly Properties

[](#writeonly-properties)

To indicate that the type should be readonly use `@property-write` annotation.

```
/**
 * @property-write $a
 */
class MyClass
{
    use Serafim\Properties\Properties;

    protected $a = 23;
}

$dto = new MyClass();
$dto->a = 42; // 42
$dto->a;      // Error: Property MyClass::$a is writeonly
```

### Getters And Setters

[](#getters-and-setters)

For getter or setter override just declare `get[Property]`(`is[Property]` for `bool` values will be works too) or `set[Property]` methods.

```
/**
 * @property-read int $a
 */
class MyClass
{
    use Serafim\Properties\Properties;

    protected $a = 23;

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

$dto = new MyClass();
echo $dto->a; // 42 (because 23 + 19 = 42)
$dto->a = 42; // Error: Property is read-only (@property-read doc declaration)
```

Setter:

```
/**
 * @property-write string $anotherProperty
 */
class Some
{
   // ...
   protected $anotherProperty = 'some';

    /**
     * @param string $newVal
     */
   public function setAnotherProperty($newVal)
   {
       // Just example
       if (mb_strlen($newVal) > 4) {
           throw new InvalidArgumentException('...');
       }

       $this->anotherProperty = $newVal;
   }
}
```

### Autocomplete

[](#autocomplete)

All these annotations fully work in the IDE, including autocomplete and highlighting incorrect behavior.

 [![IDE Autocomplete And Highlighting](https://camo.githubusercontent.com/6dd4c06db8b2ea8a19a57b5c173798af41146a086d02706e4cc7ae5c34be4363/68747470733a2f2f686162726173746f726167652e6f72672f776562742f5f702f756b2f75762f5f70756b7576743665786f2d78767779727a7032626938753072652e706e67)](https://camo.githubusercontent.com/6dd4c06db8b2ea8a19a57b5c173798af41146a086d02706e4cc7ae5c34be4363/68747470733a2f2f686162726173746f726167652e6f72672f776562742f5f702f756b2f75762f5f70756b7576743665786f2d78767779727a7032626938753072652e706e67)

Type Hints
----------

[](#type-hints)

All properties with writeable behavior will be "type checkable".

```
/**
 * @property int|null $a
 */
class Some
{
    use Serafim\Properties\Properties;

    protected $a;
}

//

$some = new Some;
$some->a = 23; // Ok
$some->a = null; // Ok
$some->a = 'string'; // Error: "TypeError: Value for property Some::$a must be of the type int|null, string given"
```

### Primitive Type Hints

[](#primitive-type-hints)

- `int` (or `integer`) - property value are integer
- `bool` (or `boolean`) - value are boolean
- `float` (or `double`) - value are float
- `string` - value are string or object with `__toString` method
- `null` (or `void`) - value are nullable
- `resource` - value are resource
- `object` - value can be any object
- `mixed` - no type checking
- `callable` - value can be string, instance of \\Closure, array with 2 args or object with `__invoke` method
- `scalar` - value cannot be an object
- `countable` - value can be a countable (array or object provided `Countable` interface).
- `self` - value can be object of self class or string with name of self class

> *`self` keyword does **not available** yet: it will be supports in future*

- `static` - value can be instance of self class or string whos are sublass of self

> *`static` keyword does **not available** yet: it will be supports in future*

- `$this` - value can be only object instance of self class

> *`$this` keyword does **not available** yet: it will be supports in future*

### Arrays And Generics

[](#arrays-and-generics)

- `array` - value is type of array
- `Class[]` - value is type of array or instance of \\Traversable
- `scalar[]` - value is type of array or instance of \\Traversable
- `Collection` - value is type of array or instance of "Collection" and \\Traversable
- `Collection` - value is type of array or instance of "Collection" and \\Traversable
- `Collection`- value is type of array or instance of "Collection" and \\Traversable

### Conjunction And Disjunction

[](#conjunction-and-disjunction)

- `a|b` - means that the value must be type `(a or b)`.
- `a&b` - means that the value must be type `(a and b)`.
- `a|b&c` - means that the value must be type `(a or (b and c))`.

See more:

Production Mode
---------------

[](#production-mode)

The code is quite effective, but in the production mode you should use caching. The package implements support for the PSR-16 standard.

```
$driver = new Psr16CacheDriver(); // Your PSR16 cache driver implementation

$properties = Serafim\Properties\Bootstrap::getInstance();
$properties->setCacheDriver($driver);
```

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 74.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 ~238 days

Total

5

Last Release

2778d ago

Major Versions

1.0.2 → 2.0.02018-10-20

PHP version history (2 changes)1.0.0PHP &gt;=5.5

2.0.0PHP ^7.1.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/150420?v=4)[Ruslan Sharipov](/maintainers/Serafim)[@serafim](https://github.com/serafim)

---

Top Contributors

[![SerafimArts](https://avatars.githubusercontent.com/u/2461257?v=4)](https://github.com/SerafimArts "SerafimArts (29 commits)")[![Lisennk](https://avatars.githubusercontent.com/u/8103985?v=4)](https://github.com/Lisennk "Lisennk (7 commits)")[![Malezha](https://avatars.githubusercontent.com/u/3649525?v=4)](https://github.com/Malezha "Malezha (2 commits)")[![roquie](https://avatars.githubusercontent.com/u/3214290?v=4)](https://github.com/roquie "roquie (1 commits)")

---

Tags

dslgettersphpphpdocpropertiessetterstype-hintfieldspropertiespsr-5

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/serafim-properties/health.svg)

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

###  Alternatives

[mobiledetect/mobiledetectlib

Mobile\_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

10.7k170.9M513](/packages/mobiledetect-mobiledetectlib)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.0k](/packages/illuminate-contracts)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k39](/packages/civicrm-civicrm-core)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

378604.0k99](/packages/flow-php-etl)[phiki/phiki

Syntax highlighting using TextMate grammars in PHP.

3693.8M51](/packages/phiki-phiki)[florianv/exchanger

PHP exchange rate provider layer for currency conversion: 30+ services, chain fallback, and caching.

1865.0M20](/packages/florianv-exchanger)

PHPackages © 2026

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