Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Przypisanie obiektu w klasie
Forum PHP.pl > Forum > PHP > Object-oriented programming
rahul
Witam. Otoz pisze sobie takie cos i wywala mi blad :/

  1.  
  2. $notification = $notification_M->getNotificationByTitle('unregistered'); // zwraca obiekt notifikacja
  3. $users = $user_M->getUnregisteredNotified($notification); // zwraca uzytwkonikow z bd + tworzy nowy obiekt notifikacja_wyslana ktora nadpisuje wczesniejsza.
  4.  
  5. class UserManager {
  6.  
  7. public function getUnregisteredNotified($notification)
  8. {
  9. $sql = mysql_query("SELECT users.id, notifications_sent.id as 'notification_sent_id' FROM users, notifications_sent
  10. WHERE users.status = 2
  11. AND users.id = notifications_sent.user_id
  12. AND notifications_sent.sent = 0
  13. AND users.time < DATE_SUB(NOW(), INTERVAL '264' DAY)
  14. ORDER BY users.time DESC
  15. ");
  16. $this->handler($sql);
  17. return $this->userContainer;
  18. }
  19.  
  20. private function handler($sql)
  21. {
  22. while($row = mysql_fetch_assoc($sql))
  23. {
  24. $user = new User();
  25. $user->setId($row['id']);
  26. $this->userContainer[] = $user;
  27.  
  28. if($row['notification_sent_id'])
  29. {
  30. notificationManager::setSentNotification($row['notification_sent_id']);
  31. }
  32. }
  33. }
  34.  
  35. }// koniec klasy userManager.
  36.  
  37. class notificationManager {
  38.  
  39.  
  40. private $sentNotification;
  41.  
  42. public function setSentNotification($param)
  43. {
  44. $this->sentNotification = new SentNotification();
  45. $this->sentNotification->setSentId($param);
  46. }
  47.  
  48. public function getSentNotification()
  49. {
  50. return $this->sentNotification;
  51.  
  52. }
  53. } // koniec klasy


Generalnie chodzi o to ze przypisuje do zmiennej private sentNotificaion klasy NotifiacionManager obiekt new SentNotification. I wszystko cacy, moge kozystac z metod tego obiektu dzieki tej referencji. Lecz jak sie odwoluje do tego obiektu spoza klasy to juz wtedy mi mowi ze jest gupi i ze " Call to a member function getSentId() on a non-object in "

  1. $dupa = $notification_M->getSentNotification()->getSentId();


Czemu tak ? Co pominalem.
nospor
Nigdzie nie widzę, gdzie inicjalizujesz obiekt $notification_M ani nigdzie nie widzę byś na tym obiekcie wykonywał setSentNotification()
Nic więc dziwnego, że getSentNotification() zwraca ci null.

php nie jest głupi i jak ci mowi, że coś nie jest obiektem, znaczy że nie jest obiektem i lepiej szukaj błędu w swoim kodzie a nie zwalasz winę na php.
rahul
Nie widzisz bowiem nie umiescilem tego w opisie. Problem glownie polega na tym ze po przypisaniu obiektu wewnatrz klasy do zmiennej private nie moge sie do niego dostac. ale aby bylo bardziej klarownie to napisze ci wszystko:
  1. $user_M= new UserManager($db);
  2. $notification_M = new notificationManager($db);
  3.  
  4. $notification = $notification_M->getNotificationByTitle('unregistered');
  5. $users = $user_M->getUnregisteredNotified($notification);
  6.  
  7. if($users)
  8. { foreach($users as $user)
  9. {
  10. $notification_M->updateSentNotification($user, $notification);
  11. $dupa = $notification_M->getSentNotification()->getSentId(); // tu wywala blad
  12. $dupa = $notification_M->sendNotification; // Tak tez nie dziala jakby co ;)
  13. }
  14. }
  15.  
  16. class notificationManager {
  17.  
  18. private $db;
  19. public $sendNotification; // TU PRZYPISUJE MOJ OBIEKT , spoza klasy nie dziala
  20.  
  21. public function __construct(Database $db)
  22. {
  23. $this->db = $db;
  24. }
  25.  
  26. public function setSentNotification($param)
  27. {
  28. $this->sendNotification= new SentNotification();
  29. $this->sendNotification->setSentId($param);
  30. print_r($this->sendNotification) // To zadziala pokaze mi wszystko co chce , ale juz spoza klasy niiii :(
  31. }
  32.  
  33. public function getSentNotification()
  34. {
  35. return $this->sendNotification;
  36. }
  37.  
  38.  
  39. public function getNotificationByTitle($title) // FETCHES ALL INFORMATION (ID, SUBJECT, CONTENT , INTERVAL ) OF NOTIFICATION
  40. {
  41. $sql = mysql_query("SELECT * FROM notifications WHERE title = '$title' ");
  42. $notification = $this->handler($sql);
  43. return $notification;
  44. }
  45.  
  46. private function handler($sql)
  47. {
  48. $row = mysql_fetch_assoc($sql);
  49.  
  50. $notification = new Notification();
  51. $notification->setId($row['id']);
  52. $notification->setSubject($row['subject']);
  53. $notification->setBody($row['body']);
  54. $notification->setInterval($row['interval']);
  55.  
  56. return $notification;
  57. }
  58.  
  59. }// koniec notificationManagera
  60.  
  61.  
  62. class UserManager {
  63.  
  64. public function getUnregisteredNotified($notification) // zwraca tez note_id
  65. {
  66. $sql = mysql_query("SELECT users.id, notifications_sent.id as 'notification_sent_id' FROM users, notifications_sent
  67. WHERE users.status = 2
  68. AND users.id = notifications_sent.user_id
  69. AND notifications_sent.sent = 0
  70. AND users.time < DATE_SUB(NOW(), INTERVAL '264' DAY)
  71. ORDER BY users.time DESC
  72. ");
  73. $this->handler($sql);
  74. return $this->userContainer;
  75. }
  76.  
  77. private function handler($sql)
  78. {
  79. while($row = mysql_fetch_assoc($sql))
  80. {
  81. $user = new User();
  82. $user->setId($row['id']);
  83. $this->userContainer[] = $user;
  84.  
  85. if($row['notification_sent_id'])
  86. {
  87. notificationManager::setSentNotification($row['notification_sent_id']); // TUTAJ wywoluje metode do stworzenia nowej Notificaji.
  88. }
  89. }
  90. }
  91.  
  92.  
  93.  
  94. }// koniec klasy
  95.  


Kurcze, to chyba wszystko , chyba ze mam jeszcze umiescic opisy kontenerow ..
nospor
Powtarzam:
Nigdzie nie wywołujesz setSentNotification() na obiekcie $notification_M

Jeśli myślisz, że to:
notificationManager::setSentNotification($row['notification_sent_id']);
załatwia sprawę to się mylisz. To coś odpala metodę statyczną klasy, która działa na $this. Jest to błędne bo mieszasz w tej chwili metody statyczne klasy z odwołaniem do obiektu klasy - to są dwie różne rzeczy.
rahul
ok, dzieki.
Widzisz , ja myslalem ze ta statyczna metoda wszystko zalatwi. Czy zatem musze przekazac obiekt notificationManager do klasu UserManager i wtedy odpalic funkcje ?
nospor
I tak i nie
TAK: składniowo wszystko będzie ok
NIE: logicznie będzie bez sensu. Przecież do setSentNotification przekazujesz ID a to chyba z każdym userem jest inne i po każdym wywołaniu będzie się nadpisywać.
rahul
bedzie sie nadpisywac to prawda, tylko jest on w petelce wiec bedzie dobrze przypisywal swoje parametry do uzytkownika w tym akurat przypadku. Ewentualnie moge go wrzucic do jakiegos kontenera w sensie array'a. tylko wtedy tez bede po nim musial przejsc jakas petla.
  1.  
  2. $notification = $notification_M->getNotificationByTitle('unregistered');
  3. $users = $user_M->getUnregisteredNotified($notification);
  4.  
  5. if($users)
  6. { foreach($users as $user)
  7. {
  8. $notification_M->updateSentNotification($user, $notification);
  9. $notification_M->InsertTimesWhenNotified($notification_M->getSentNotification()->getId);
  10. }
  11. }
  12.  
  13.  
  14.  


A moje pytanie jeszcze takie dodatkowe - czy jakims prostym sposobem moge bez problemu odziedziczyc wszystkie wartosci poprzedniej notyfikacji do tej nowej czy musze je od nowa przypisywac :/
Chce tylko dodac jeden parametr a reszte zostawic.

To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.