Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PDO][OOP] Przekazanie handlera połączenia do drugiej klasy
Raven1122
post 17.04.2013, 11:35:09
Post #1





Grupa: Zarejestrowani
Postów: 369
Pomógł: 2
Dołączył: 1.11.2010

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


Witam,

Mam klasę obsługującą PDO:

connection.class.php:

  1. class Connection{
  2.  
  3. public function __construct(){
  4. try {
  5. $db = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
  6. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  7. } catch(PDOException $e) {
  8. echo 'ERROR: ' . $e->getMessage();
  9. }
  10. }


^ To tylko fragment potrzebny

I klase obsługującą renderowanie planu:

schedule.php

  1. <?php
  2.  
  3. require('../pdo.class.php');
  4.  
  5. //Plik wyciągający dane ramówki
  6.  
  7. class ScheduleRendering extends Connection{
  8.  
  9. public function fetch($db){
  10. $this->day = $_GET['day'];
  11.  
  12. $stmt = $db->prepare('SELECT * FROM schedule WHERE day = ?');
  13. $stmt->bindValue(1, $this->day);
  14. $stmt->execute();
  15. $contents = $stmt->fetchAll();
  16. return $contents;
  17. }
  18.  
  19. public function render($contents){
  20. $this->row = 0;
  21. $this->rows = '';
  22. foreach($contents as $row){
  23. $this->rows .= '<tr><td>'.$row['name'].'</td><td>'.$row['presenter'].'</td><td>'.$row['times'].' - '.$row['timed'].'</td></tr>';
  24. }
  25.  
  26. $rows = $this->rows;
  27. return $rows;
  28. }
  29.  
  30. }
  31. $db = new Connection();
  32. $schedule = new ScheduleRendering();
  33. $schedule->fetch($db);
  34. $schedule->render($contents);
  35.  
  36. echo $rows;
  37.  
  38. ?>


No i mam taki problem, bo z pliku schedule wywala mi Fatal Error:

Fatal error: Call to undefined method Connection::prepare() w linii 12 plik schedule.php

Jak przekazać handler $db do metody fetch klasy Schedule??
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 14.06.2025 - 01:30