Skip to content

Instantly share code, notes, and snippets.

@ejunker
Created November 12, 2018 14:42
Show Gist options
  • Save ejunker/d05935d17b3f3d9e8ee63399fb0c76a9 to your computer and use it in GitHub Desktop.
Save ejunker/d05935d17b3f3d9e8ee63399fb0c76a9 to your computer and use it in GitHub Desktop.
Read-only properties with IDE autocomplete
<?php
/**
* @property-read string $name
* @property-read int $age
*/
class Person
{
private $name;
private $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function __get($name)
{
return $this->{$name};
}
/**
* @param $name
* @param $value
*
* @throws \Exception
*/
public function __set($name, $value)
{
throw new \Exception('Cannot mutate properties');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment