Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Publiczny modyfikator dostępu..., ...a może jednak korzystać?
starach
post
Post #1





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

Ostrzeżenie: (0%)
-----


Cześć,

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:
  1. class PropertiesInjector
  2. {
  3. /**
  4. * array('id' => array('type' => 'integer'),
  5. * 'name' => array('type' => 'string'),
  6. * 'ip' => array('length' => <int>))
  7. * @var array
  8. */
  9. protected $aPatterns;
  10. /**
  11. * Stores properties and their values
  12. * @var array
  13. */
  14. protected $aProperties;
  15. /**
  16. * Returns class property or throws exception if it does not exists
  17. * @param string $sName
  18. * @return mixed
  19. */
  20. public function __get($sName)
  21. {
  22. $this->_propertyExists($sName);
  23. return isset($this->aProperties[$sName]) ? $this->aProperties[$sName] : null;
  24. }
  25. /**
  26. * Sets class property value or throws an error if value
  27. * is other type than declared or property does not exists
  28. * @param string $sName
  29. * @param mixed $mValue
  30. */
  31. public function __set($sName, $mValue)
  32. {
  33. $this->_propertyExists($sName);
  34.  
  35. if(isset($this->aPatterns[$sName]['type']))
  36. {
  37. $aTypes = $this->aPatterns[$sName]['type'];
  38. $aTypes = \is_array($aTypes) ? $aTypes : array($aTypes);
  39. if(false === \array_search(gettype($mValue), $aTypes))
  40. {
  41. throw new Exception\PropertyValue($sName, $mValue);
  42. }
  43. }
  44. if(isset($this->aPatterns[$sName]['length']))
  45. {
  46. if(\strlen($mValue) > $this->aPatterns[$sName]['length'])
  47. {
  48. throw new Exception\PropertyToLong($sName, $mValue, $this->aPatterns[$sName]['length']);
  49. }
  50. }
  51. $this->aProperties[$sName] = $mValue;
  52. }
  53. /**
  54. * Throws exception if property name is incorrect
  55. * @param string $sName
  56. */
  57. private function _propertyExists($sName)
  58. {
  59. if(!isset($this->aPatterns[$sName])) {
  60. throw new Exception\PropertyName($sName);
  61. }
  62. }
Tak więc się zastanawiam jak to teraz powinno być z tymi modyfikatorami dostępu. Co o tym sądzicie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
starach
post
Post #2





Grupa: Zarejestrowani
Postów: 999
Pomógł: 30
Dołączył: 14.01.2007
Skąd: wiesz ?

Ostrzeżenie: (0%)
-----


__isset owszem z tym że chciałbym żeby w przypadku nie istniejącej właściwości skrypt płuł mi wyjątkiem czego wydaje mi się nie należy robić przez __isset żeby dać możliwość programiście sprawdzenia czy dana właściwość istnieje.

  1. /**
  2. * Checks whether class property exists or not
  3. * @param string $sName
  4. * @return boolean
  5. */
  6. public function __isset($sName)
  7. {
  8. return isset($this->aPatterns[$sName]);
  9. }
  10. /**
  11. * Throws exception if property name is incorrect
  12. * @param string $sName
  13. */
  14. private function _propertyExists($sName)
  15. {
  16. if(!isset($this->$sName)) {
  17. throw new Exception\PropertyName($sName);
  18. }
  19. }
Czyli nie ma żadnych przeciwskazań. Super.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 10.06.2026 - 12:13