Skip to content

Support for Read Only properties

Choose a tag to compare

@g105b g105b released this 12 Jul 11:08
1075f8d

What's changed:

  • ReadOnly properties act as bind getters by default by @g105b in #355

In this minor release, classes of the application can now take advantage of PHP 8.1's new readonly keyword. Any properties that are marked as public readonly will be exposed as Bindable values now, removing a lot of boilerplate code on Model classes.

Example:

// Model class:
class PersonModel {
	public function __construct(
		public readonly string $id,
		public readonly string $name,
		public readonly int $age,
	) {}

	#[BindGetter]
	public function getAgeStatus():string {
		return $this->age >= 18
			? "adult"
			: "minor";
	}
}
// Page code:
function go(Input $input, DocumentBinder $binder, PersonRepository $personRepo):void {
	$binder->bindData($personRepo->getById($input->getString("id"));
}
<!-- Page view -->
<div class="user-profile">
	<h1 data-bind:text="name">User name<h1>
	<img src="/asset/img/user-profile/{{id}}.jpg" alt="{{name}}'s user profile" />
	<p>This user is marked as <span data-bind:text="ageStatus">Age Status</span></p>
</div>