Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> __get $_GET, $_POST, pobieranie danych od usera
dr_bonzo
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Zamiast spac wpadlem na ciekawy pomysl i napisalem cos takiego:
  1. <?php
  2. class UserData
  3. {
  4. private $arrValues = array();
  5.  
  6. public UserData( $strSource )
  7. {
  8. $this->strSource = strval( $strSource );
  9. }
  10.  
  11. public __get( $strValName )
  12. {
  13. if ( isset( $this->arrValues[ $strValName ] ) )
  14. {
  15. if ( is_null( $this->arrValues[ $strValName ] ) )
  16. {
  17. throw new Exception( 'Value not present' );
  18. }
  19. else
  20. {
  21. return $this->arrValues[ $strValName ];
  22. }
  23. }
  24. else
  25. {
  26. $arrSuperGlobalArray = NULL;
  27.  
  28. switch ( $this->arrValues )
  29. {
  30. case 'get':
  31. {
  32. $arrSuperGlobalArray &= $_GET;
  33. break;
  34. }
  35.  
  36. case 'post':
  37. {
  38. $arrSuperGlobalArray &= $_POST;
  39. break;
  40. }
  41.  
  42. case 'cookie':
  43. {
  44. $arrSuperGlobalArray &= $_COOKIE;
  45. break;
  46. }
  47.  
  48. default :
  49. {
  50. throw new Exception( 'Invalid source type '' . $this->strSource . ''' );
  51. }
  52. }
  53.  
  54. if ( isset( $arrSuperGlobalArray[ $strValName ] ) )
  55. {
  56. return $arrSuperGlobalArray[ $strValName ];
  57. }
  58. else
  59. {
  60. $this->arrValues[ $strValName ] = NULL;
  61. }
  62. }
  63. }
  64. }
  65.  
  66. $objGetValues = new UserData( 'get' );
  67. print( $objGetValues->action );
  68.  
  69. $objPostValues = new UserData( 'post' );
  70. print( $objPostValues->id );
  71.  
  72. ?>


Nowy sposob pobierania danych wykorzystujacy php5. Nie testowany -- nie mam serwera pod reka. Co o tym sadzicie?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Imperior
post
Post #2





Grupa: Zarejestrowani
Postów: 105
Pomógł: 0
Dołączył: 16.10.2004

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


1. Brak deklaracji $this->strSource
2. Niedopracowanie/nieskończone: nie ma możliwości aby:
  1. <?php
  2. isset( $this->arrValues[ $strValName ] )
  3. ?>

było prawdziwe, a zarazem:
  1. <?php
  2. is_null( $this->arrValues[ $strValName ] )
  3. ?>

nie prawdziwe.
3. Korzystasz z $this->arrValues w switchu zamiast $this->strSource
4. Ta klasa nic nie robi, czy to jest podstawa, którą można jakoś rozszerzać? Jeśli tak, to pola jako protected powinny być.
5. Jeśli to ma być rozszerzane, to może jakiś dekorator by się przydał?
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: 27.12.2025 - 12:53