Skip to content

Instantly share code, notes, and snippets.

@dlundgren
Created July 30, 2015 07:11
Show Gist options
  • Save dlundgren/4b6abefeecfd1241d5e7 to your computer and use it in GitHub Desktop.
Save dlundgren/4b6abefeecfd1241d5e7 to your computer and use it in GitHub Desktop.
Idea for entities to act more java like with regards to private setters and public access to the properties
<?php
namespace Domain\Shared;
// create this separately
use Domain\Exception\InvalidProperty;
use Domain\Exception\Immutable;
/**
* Allows public access to the private properties of an object.
*
* Prevents external modification to the properties. Reflection and objects are the most likely exceptions to this.
*/
trait ImmutableEntity
{
/**
* Tests if the property is set
*
* @param string $name
* @return bool
*/
public function __isset($name)
{
return (property_exists($this, $name) && isset($this->$name));
}
/**
* Returns the property
*
* @param string $name
* @return bool
*/
public function __get($name)
{
if (property_exists($this, $name)) {
return $this->$name;
}
throw new InvalidProperty($name);
}
/**
* Sets the property
*
* @param string $name
* @return bool
*/
public function __set($name, $value)
{
throw new Immutable();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment