Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasa w klasie?
Tarcil
post
Post #1





Grupa: Zarejestrowani
Postów: 41
Pomógł: 0
Dołączył: 24.11.2006

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


Witam serdecznie!

Od jakiegoś czasu próbuję opanować OOP w PHP i idzie mi, nie przymierzając, słabo. Mój problem polega na tym, że nie potrafię z jednej klasy odwołać się do metod innej. Nie wiem, czy to nie jest przypadkiem durnota jakaś i mój błąd w próbie myślenia obiektowo... proszę o pomoc w każdym bądź razie.

Mam klasę MySqlEng(). Konstruktor tej klasy tworzy połączenie z bazą danych i zapisuje uchwyt połączenia w prywatnej zmiennej klasy. Dalej mam 4 metody: selectQuery, updateQuery, insertQuery i deleteQuery, które przygotowują w prywatnej zmiennej $query string, który metodzie doQuery() służy do wykonania odpowiedniego zapytania w bazie danych. Dochodzi jeszcze metoda fetchResults(), która pozwala na przewijanie rekordów uzyskanych w odpowiedzi na zapytanie.

Standardowo używam tej klasy tak:
  1. <?php
  2. $sql = new MySqlEng();
  3.  
  4. $sql -> selectQuery('nazwa, ilosc', 'produkty', 'id > 93');
  5. while($res = $sql->fetchResults())
  6. {
  7. echo $res['nazwa'].' pozostała ilość: '.$res['ilosc'].'<br />';
  8. }
  9. ?>


Próbuję teraz złożyć klasę pagesSupport(), której zadaniem będzie: dodawanie stron do bazy danych, ich edycja i zmiana, oraz wyciągnie z bazy danych o odpowiedniej podstronie i wyswietlanie jej na ekranie. Zwykłe użycie wewnątrz metody tej klasy metody $sql->selectQuery() wywołuje błąd...

Jak skorzystać z klasy MySqlEng() wewnątrz klasy pagesSupport()(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Podaję też kod klasy MySqlEng():
  1. <?php
  2. //config
  3.    //this data should be inserted in another file, which is not available  by http://
  4.    define('DATABASE_HOST', 'localhost');
  5.    define('DATABASE_USER', 'tarcil_gsms');
  6.    define('DATABASE_PASS', 'gsms123');
  7.    define('DATABASE_NAME', 'tarcil_gsms');
  8.    
  9.    class MySqlEng
  10.        {
  11.        private $handler;
  12.        public $query;
  13.        private $results;
  14.        private $affected_rows;
  15.        private $last_row_id;
  16.        private $selected_rows;
  17.        public $records = array();
  18.        
  19.        
  20.        public function __construct()
  21.            {
  22.            if(@!$sql_handler = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS))
  23.                {
  24.                throw new Exception('Problem z połączeniem do bazy danych.<br />Nr błędu: <b>'.mysql_errno().'</b><br /><b>Komunikat: </b>'.mysql_error());
  25.                }
  26.            else
  27.                {
  28.                if(!mysql_select_db(DATABASE_NAME))
  29.                    throw new Exception('Problem z wybraniem bazy danych.<br />Nr błędu: <b>'.mysql_errno().'</b><br /><b>Komunikat: </b>'.mysql_error());
  30.                else
  31.                    {
  32.                    //connection successfull:
  33.                    $this->handler = $sql_handler;
  34.                    unset($sql_handler);
  35.                    }
  36.                }
  37.            }
  38.            
  39.        //next four methods create sql query for doQuery method;
  40.        public function selectQuery($fields, $table, $where = "", $order = "", $limit = "")
  41.            {            
  42.            //if is another query saved:
  43.            if($this->query) unset($this->query);
  44.            
  45.            //creating query for doQuery() method
  46.            $this->query = 'SELECT '.$fields.' FROM '.$table;
  47.            if($where)
  48.                $this->query .= ' WHERE '.$where;
  49.            if($order)
  50.                $this->query .= ' ORDER BY '.$order;
  51.            if($limit)
  52.                $this->query .= ' LIMIT '.$limit;
  53.                
  54.            $this->doQuery();
  55.            $this->selected_rows = mysql_num_rows($this->results);
  56.            }
  57.            
  58.        TUTAJ SĄ JESZCZE TRZY METODY DO USTAWIANIA ZMIENNEJ QUERY... (updateQuery, deleteQuery, insertQuery)
  59.  
  60.            
  61.        //here is private method which will really make operations in db. it gets no arguments - all are written in object properties.
  62.        
  63.        private function doQuery()
  64.            {
  65.            if(!$this->results = mysql_query($this->query, $this->handler))
  66.                {
  67.                throw new Exception('Problem z wywołaniem zapytania do bazy danych.<br />Nr błędu: <b>'.mysql_errno().'</b><br /><b>Komunikat: </b>'.mysql_error());
  68.                }
  69.            }
  70.        
  71.        public function fetchResults()
  72.            {
  73.            if($this->results)
  74.                {
  75.                $this->records = mysql_fetch_array($this->results, MYSQL_ASSOC);
  76.                
  77.                if(is_array($this->records))
  78.                    {
  79.                    return $this->records;
  80.                    }
  81.                }
  82.            }
  83.            
  84.        public function getAffectedRows()
  85.            {
  86.            return $this->affected_rows;
  87.            }
  88.        public function getSelectedRows()
  89.            {
  90.            return $this->selected_rows;
  91.            }
  92.        public function getLastRowId()
  93.            {
  94.            return $this->last_row_id;
  95.            }
  96.        
  97.        public function __destruct()
  98.            {
  99.            mysql_close($this->handler);
  100.            }
  101.        }
  102. ?>
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: 22.08.2025 - 15:49