Created
July 30, 2015 07:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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