Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z dziedziczeniem, wywoływanie metod klas bazowych
asqwe
post
Post #1





Grupa: Zarejestrowani
Postów: 4
Pomógł: 0
Dołączył: 22.08.2008

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


Witam,

mam problem z dziedziczeniem w PHP5. Konkretnie chodzi o wywoływanie metod klas bazowych. Mam dwie klasy - sql i sesssion. Chciałbym, aby klasa session korzystała z klasy sql przy operacjach na bazie mysql.



Klasa sql:
Kod
       <?
       class sql {
      
         public $_res;
         public $_sql;
         public function __construct() {
        
           // otwiera połączenie z bazą
          
           if(!$this->_sql) {
             include_once("config.php");
            
             @ $this->_sql = new mysqli($HOST, $USER, $PASS, $DB);
             @ $this->_sql -> query("SET NAMES latin2");
            
            
             if (mysqli_connect_errno() != 0){
                  throw new Exception(mysqli_connect_error());
             }
            
           }
        
         }
      
         public function query($zapytanie) {
        
           // zwraca wartość mysqli
          
           $this->_res = $this->_sql->query($zapytanie);
           $ile = $this->_res -> num_rows;
          
           if (mysqli_errno() != 0) {
               throw new Exception(mysqli_error());
           }
        
           return $this->_res;
        
         }
  
     public function numRows() {
       return $this->_res->num_rows;
     }
    
        
      
       }
     ?>



Klasa sesssion
Kod
       <?
       include_once("sql.class.php");
       session_start();
       class session extends sql
       {
         private $sessionTime = 300; // czas po jakim wylgoować nieaktywnych
         private $logs = array(); // tablica logów
      
         public function __construct()
         {
        
           parent::query("DELETE FROM sessions WHERE last < CURRENT_TIMESTAMP()+".$this->sessionTime); // wylogowuje nieaktywnych
      
           // sprawdza czy sesja jest nadal ważna
           if($_SESSION[auth] == 1 && strlen($_SESSION[token]) == 16 && is_int($_SESSION[id]) && $_SESSION[ip] == $_SERVER[REMOTE_ADDR])
           {
             parent::query("SELECT * FROM `sessions` WHERE `token` = '$_SESSION[token]' AND `id` = '$_SESSION[id]' AND `ip` = '$_SESSION[ip]'");
             if(parent::numRows != 1) $_SESSION[auth] = FALSE;
             else echo "Jesteś zalogowany
       ip: $_SESSION[ip]";
           }
           else
           { echo("Nie jesteś zalogowany!"); $_SESSION[auth] = 0; }
         }
  
     public function addLog($action)
     { array_push($this->logs,strip_tags($action)); }
    
     public function __destruct()
     {
       $query = "INSERT INTO logs (`action`) VALUES ";
       foreach($this->logs as $logs)
       { $query .= "('$logs'), "; }
       parent::query(substr($query,0,-2).";");
     }
   }
       ?>


Plik w którym testuję klasę:
Kod
<?
session_start();
include_once("session.class.php");

try {

   $obj = new session();
   $obj -> addLog("1");
   $obj -> addLog("<b>2</b>");
   $obj -> addLog("3");
  
}

catch(Exception $error) {

   echo("<p class='error'>Wystąpił problem: ".$error->GetMessage()."
Plik: ".$error->GetFile()."
Wierz: ".$error->GetLine()."</p>");
   include("foot.php");
   exit;
  
}

include("foot.php");
?>


Ten post edytował asqwe 22.08.2008, 09:43:43
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: 24.12.2025 - 15:43