PHPackages                             phpgt/propfunc - 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. phpgt/propfunc

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

phpgt/propfunc
==============

Property accessor and mutator functions.

v1.0.1(5y ago)13.6M↓42.1%[2 PRs](https://github.com/PhpGt/PropFunc/pulls)3MITPHPPHP &gt;=8.0CI passing

Since Feb 17Pushed 3w ago1 watchersCompare

[ Source](https://github.com/PhpGt/PropFunc)[ Packagist](https://packagist.org/packages/phpgt/propfunc)[ GitHub Sponsors](https://github.com/sponsors/PhpGt)[ RSS](/packages/phpgt-propfunc/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (2)Versions (15)Used By (3)

Property accessor and mutator functions.
========================================

[](#property-accessor-and-mutator-functions)

Property accessors and mutators are commonly referred to as "getter" and "setter" functions. This library uses [PHP's Magic Methods](https://www.php.net/manual/en/language.oop5.magic.php) to easily hook up getter and setter functions that are exposed externally as normal properties, via the `MagicProp` trait.

Why? This kind of functionality can certainly be seen as a hack, but sometimes hacks are necessary. Specifically PHP.GT is implementing [the DOM standard in PHP](https://www.php.gt/dom) which requires certain properties to have "live" or "readonly" functionality, which is only possible using magic \_\_get and \_\_set functions, in the supported PHP versions. This library simply holds the reusable behaviour for other repositories that require it.

Note

Since PHP 8.4, [object property hooks](https://www.php.net/manual/en/language.oop5.property-hooks.php) were introduced, which will eventually render this library obsolete.

---

[ ![Build status](https://camo.githubusercontent.com/3fc020e8d87f71dd7254485b3c61c280c5f268d0e700b519feadbd313e1a184a/68747470733a2f2f62616467652e7374617475732e7068702e67742f70726f7066756e632d6275696c642e737667)](https://github.com/PhpGt/PropFunc/actions)[ ![Code quality](https://camo.githubusercontent.com/11fd0d1063719c3cb6e1a84b2ace26824bbbfee70fbbe66f0255c3eb7616a385/68747470733a2f2f62616467652e7374617475732e7068702e67742f70726f7066756e632d7175616c6974792e737667)](https://scrutinizer-ci.com/g/PhpGt/PropFunc)[ ![Code coverage](https://camo.githubusercontent.com/0db49176c9d60ec627d9be63e9f3616e71748249a722b28d72902c9141d6972b/68747470733a2f2f62616467652e7374617475732e7068702e67742f70726f7066756e632d636f7665726167652e737667)](https://scrutinizer-ci.com/g/PhpGt/PropFunc)[ ![Current version](https://camo.githubusercontent.com/05c65dd00546e99f0b8f97a57505860487eac819225d427226b75d54025c6e7b/68747470733a2f2f62616467652e7374617475732e7068702e67742f70726f7066756e632d76657273696f6e2e737667)](https://packagist.org/packages/PhpGt/PropFunc)[ ![PHP.GT/PropFunc documentation](https://camo.githubusercontent.com/5a1fd230a982f5ce5011c62f1f5db86e5d4e7b6dbae89829a85ba52c6de83236/68747470733a2f2f62616467652e7374617475732e7068702e67742f70726f7066756e632d646f63732e737667)](http://www.php.gt/propfunc)Example usage: Read-only properties that are calculated upon access
-------------------------------------------------------------------

[](#example-usage-read-only-properties-that-are-calculated-upon-access)

See the class `Day` below, which represents a day in time:

```
use GT\PropFunc\MagicProp;

/**
 * @property-read bool $future True if the day is in the future
 * @property-read int $daysApart Days between now and this day
 */
class Day {
	use MagicProp;

	public function __construct(
		private DateTimeInterface $dateTime
	) {}

// Expose the "dateTime" private property with read-only access:
	private function __prop_get_dateTime():DateTimeInterface {
		return $this->dateTime;
	}

// Expose the "future" calculated property with read-only access:
	private function __prop_get_future():bool {
		$now = new DateTime();
		return $now < $this->dateTime;
	}

// Expose the "daysApart" calculated property with read-only access:
	private function __prop_get_daysApart():int {
		$now = new DateTime();
		$diff = $now->diff($this->dateTime);
		return $diff->days;
	}
}
```

See the code below which uses the `Day` class. It can access the properties, but not mutate them.

```
$day = new Day($dateTime);
echo "Day is $day->diff days in the ";
echo $day->future ? "future" : "past";
echo PHP_EOL;
$day->diff = 10;
echo "Exception thrown on line above!";
```

Usages
------

[](#usages)

- Read only properties - as with the above example, properties can be made read-only. This feature is [coming to the PHP language](https://wiki.php.net/rfc/write_once_properties), but without the ability to define the accessor logic.
- Live properties - if a property's value is required to update depending on certain conditions, a getter function is required.
- Property validation - if a property's value can't simply be validated by its type alone, the setter function can be used to ensure the value meets validation criteria.

Proudly sponsored by
====================

[](#proudly-sponsored-by)

[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)

[![JetBrains logo.](https://camo.githubusercontent.com/b5639e7738c6dfae9fe3f3e20175570b7376ce2577a772e09c25c2d4f14bf86e/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a6574627261696e732e737667)](https://www.jetbrains.com/community/opensource/)

###  Health Score

51

—

FairBetter than 95% of packages

Maintenance62

Regular maintenance activity

Popularity44

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity66

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

Total

4

Last Release

1928d ago

Major Versions

v0.1.0 → v1.0.02021-03-23

PHP version history (2 changes)v0.0.1PHP &gt;=7.4

v1.0.0PHP &gt;=8.0

### Community

Maintainers

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

---

Top Contributors

[![g105b](https://avatars.githubusercontent.com/u/358014?v=4)](https://github.com/g105b "g105b (7 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgt-propfunc/health.svg)

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

###  Alternatives

[webmozart/glob

A PHP implementation of Ant's glob.

26138.8M146](/packages/webmozart-glob)[astrotomic/laravel-dns

317.9k](/packages/astrotomic-laravel-dns)

PHPackages © 2026

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