Ciao a tutti. Ho letto di recente l'articolo Validare i moduli con PHP.
Volevo incominciare a programmare ad oggetti,quindi ho creato una classe per la connessione di php ad un db. e questo e' quello che ho fatto,mi dite se sbaglio qualcosa o se sono sulla strada giusta?
<?php
/*
Classe che rappresenta una connessione al db
*/
class ConnDb extends AbstrErrHandler
{
var $server;
var $db_name;
function ConnDb($server,$db_name)
{
parent::AbstrErrHandler();
$this->server=$server;
$this->db_name=$db_name;
}
function connetti()
{
switch($this->server)
{
case "MySQL":
$db_host="host";
$db_user="utente";
$db_password="password";
if(empty($this->db_name))
{
$this->errors[] = sprintf(IS_REQUIRED, $this->db_name);
return;
}
$conn=mysql_connect($db_host,$db_user,$db_password );
if(!$conn)
{
$this->errors[] = sprintf(NOT_CONNECT, $this->server);
return;
}else{
$this->seleziona_db($conn);
return $conn;
}
break;
default:
$this->errors[] = sprintf(BAD_SERVER, $this->server);
return;
}
}
function seleziona_db($conn)
{
$this->conn=$conn;
if(!$this->conn)
{
return;
}
switch($this->server)
{
case "MySQL":
if(!mysql_select_db($this->db_name,$this->conn))
{
$this->errors[] = sprintf(NOT_CONNECT, $this->db_name);
return;
}
break;
default:
$this->errors[] = sprintf(BAD_SERVER, $this->server);
return;
}
return;
}
function disconnetti($conn)
{
$this->conn=$conn;
if(!$this->conn)
{
return;
}
switch($this->server)
{
case "MySQL":
mysql_close($this->conn);
break;
default:
$this->errors[] = sprintf(BAD_SERVER, $this->server);
return;
}
return;
}
}// End class
Che ne dite? ..poi avrei delle domande...
Ciao grazie!!!!