Tak się zastanawiałem ostatnio nad zmianą przyzwyczajeń jeśli chodzi o tą sprawę. Deklarowanie wszystkich składowych jako prywatnych lub chronionych jest ni mniej ni więcej upierdliwe jak pisanie setterów i getterów za pomocą __get() __set(), ale estety trochę mam wrażenie na tym cierpi. Wyskrobałem sobie przed chwilą następujący kod:
Tak więc się zastanawiam jak to teraz powinno być z tymi modyfikatorami dostępu. Co o tym sądzicie?
class PropertiesInjector { /** * array('id' => array('type' => 'integer'), * 'name' => array('type' => 'string'), * 'ip' => array('length' => <int>)) * @var array */ protected $aPatterns; /** * Stores properties and their values * @var array */ protected $aProperties; /** * Returns class property or throws exception if it does not exists * @param string $sName * @return mixed */ public function __get($sName) { $this->_propertyExists($sName); } /** * Sets class property value or throws an error if value * is other type than declared or property does not exists * @param string $sName * @param mixed $mValue */ public function __set($sName, $mValue) { $this->_propertyExists($sName); { $aTypes = $this->aPatterns[$sName]['type']; { throw new Exception\PropertyValue($sName, $mValue); } } { { throw new Exception\PropertyToLong($sName, $mValue, $this->aPatterns[$sName]['length']); } } $this->aProperties[$sName] = $mValue; } /** * Throws exception if property name is incorrect * @param string $sName */ private function _propertyExists($sName) { throw new Exception\PropertyName($sName); } }