Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [OOP] 'Budowa interfejsów bazodanowych'.
MagnuM
post
Post #1





Grupa: Zarejestrowani
Postów: 108
Pomógł: 0
Dołączył: 7.05.2004
Skąd: Jelenia Góra

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


Witam,
przeczytałem artykuł ze strony głównej pt. 'Budowa interfejsów bazodanowych' i zaintrygowała mnie kwestia dziwnych funkcji zawartych w umieszczonych tam klasach. Z resztą widziałem podobne w innych artykułach.

Przytocze tutaj taki przykład z sekcji artykułu zatytułowanej 'Składamy wszystko w całość'.

  1. <?php
  2. /* Notes:
  3.    $db - instance of ADOdb connection object.
  4.    $user - instance of the User DB Interface
  5.    $topic - instance of the Topic DB Interface
  6. */
  7. $db->StartTrans();
  8.  
  9. // update the user's total # of posts
  10. $user->setNumPosts($user->getNumPosts() + 1); // PRZYKŁAD
  11. $user->submit($db); // PRZYKŁAD
  12.  
  13. // update the topics # of messages
  14. $topic->setNumMessages($topic->getNumMessage() + 1);
  15. $topic->submit($db);
  16.  
  17. // create the new message
  18. $message = new Message();
  19. $message->setTopicID($topic->getTopicID());
  20. $message->setTopic($_POST['userTopic']);
  21. $message->setMessage($_POST['message']);
  22. $message->setPoster($user->getUserID());
  23. $message->submit($db);
  24.  
  25. $db->CompleteTrans();
  26.  
  27. ?>


Jaki jest sens używania tego typu funkcji jak tutaj: setNumPosts(), getNumPosts(), submit() ?
Wydaje mi się że kod:

  1. <?php
  2. $user->setNumPosts($user->getNumPosts() + 1);
  3. $user->submit($db);
  4. ?>


Można z powodzeniem zapisać tak:
  1. <?php
  2. $user->numPosts += 1; // przyjmujac ze w konstruktorze do zmiennej $numPosts wczytywana jest ilsoc posto
    w uzytkownika
  3. ?>


A później w destruktorze wysyłać do bazy zgromadzone przez cały czas działania skryptu dane.

Pozdrawiam.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
bigZbig
post
Post #2





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


@hwao

Mowisz i masz
  1. <?php
  2.  
  3. class User
  4. {
  5. private static $_aProprietes = array();
  6.  
  7. public function __construct() {
  8. $this->_aProprietes = array('Email' => 'info@domain.com',
  9.  'Name' => 'No name');
  10. }
  11.  
  12. private function _setEmail($sEmail)
  13. {
  14. if (Filter::isEmail($sEmail) !== true) {
  15. throw new ClientException('e-mail is not valid');
  16. };
  17. $this->_aProprietes['Email'] = $sEmail;
  18. }
  19.  
  20. private function _getName()
  21. {
  22. return 'My name is ' . $this->_aProprietes['Name'];
  23. }
  24.  
  25. public function __set($sProperty, $mValue){
  26. if(method_exists($this, '_set'.$sProperty)) {
  27. call_user_func(array($this,'_set'.$sProperty), $mValue);
  28. return true;
  29. }
  30. if(!array_key_exists($sProperty, $this->_aProprietes)) {
  31. throw new Exception('Undefined property '.$sProperty.'!');
  32. }
  33. $this->_aProprietes[$sProperty] = $mValue;
  34. return true;
  35. }
  36.  
  37. public function __get($sProperty){
  38. if(method_exists($this, '_get'.$sProperty)) {
  39. return call_user_func(array($this,'_get'.$sProperty));
  40. }
  41. if(!array_key_exists($sProperty, $this->_aProprietes)) {
  42. throw new Exception('Undefined property '.$sProperty.'!');
  43. }
  44. return $this->_aProprietes[$sProperty]
  45. }
  46. }
  47.  
  48. $oU = new User();
  49. $oU->Name = 'Bond';
  50. $oU->Email = 'bond@domain.com';
  51.  
  52. echo $oU->Master;
  53. echo $oU->Email;
  54.  
  55. ?>
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: 6.10.2025 - 16:17