|
The Classes are a maximum exponent of the Programming Faced to Objects (POO). PHP is not a language faced to object, but it implements the characteristics that allow to define the classes.
But: what sound the Classes and so that they serve?. Let's start in the second, they serve to do the most legible code, and what is more important, reusable. To write a Class is undoubtedly longer that to write the code straight, but in the long run it is more profitable for his portability to other applications and his maintenance.
The Classes are only a series of variables and functions that they describe and act on something. For example, we are going to create the motor class, which will have diverse variables, $color, $modelo, $marca, $potencia, $matricula and there will be a series of functions that will act on the motor class like Price (), to Accelerate (), to Brake (), to Turn () and to Repair ().
As example we are going to create the class mysql, that will serve to us to realize consultations to the databases MySQL.
<? php
class DB_mysql {
/* connection variables */
var $BaseDatos;
var $Servidor;
var $Usuario;
var $Clave;
/* identifier of connection and consultation */
var $Conexion_ID = 0;
var $Consulta_ID = 0;
/* number of error and text error */
var $Errno = 0;
var $Error = "";
/* Construction method: Whenever we believe a variable
of this class, this function will be executed */
function DB_mysql ($bd = "", $host = "localhost", $user = "nobody", $pass = "") {
$this-> BaseDatos = $bd;
$this-> Servant = $host;
$this-> User = $user;
$this-> Key = $pass;
}
/*Conexión to the base of datos* /
function to tune in ($bd, $host, $user, $pass) {
if ($bd! = "") $this-> BaseDatos = $bd;
if ($host! = "") $this-> Servant = $host;
if ($user! = "") $this-> User = $user;
if ($pass! = "") $this-> Key = $pass;
//We connect the servant
$this-> Conexion_ID = mysql_connect ($this-> Servant, $this-> User, $this-> Key);
if (! $this-> Conexion_ID) {
$this-> Error = "Has trumped the connection.";
return 0;
}
//we select the database
if (!@mysql_select_db ($this-> BaseDatos, $this-> Conexion_ID)) {
$this-> Error = "Impossible to open".$this-> BaseDatos;
return 0;
}
/* If we have been successful tuning in he is sick
the identifier of the connection, but he returns 0 */
return $this-> Conexion_ID;
}
/* He executes a consultation */
function it consults ($sql = "") {
if ($sql == "") {
$this-> Error = "has not specified a consultation SQL";
return 0;
}
//we execute the consultation
$this-> Consulta_ID = @mysql_query ($sql, $this-> Conexion_ID);
if (! $this-> Consulta_ID) {
$this-> Errno = mysql_errno ();
$this-> Error = mysql_error ();
}
/* If we have been successful in the consultation he is sick
the identifier of the connection, but he returns 0 */
return $this-> Consulta_ID;
}
/* He returns the number of fields of a consultation */
function numcampos () {
return mysql_num_fields ($this-> Consulta_ID);
}
/* He returns the number of records of a consultation */
function numregistros () {
return mysql_num_rows ($this-> Consulta_ID);
}
/* He returns the name of a field of a consultation */
function nombrecampo ($numcampo) {
return mysql_field_name ($this-> Consulta_ID, $numcampo);
}
/* It shows the information of a consultation */
function verconsulta () {
I begin "<table border=1> n";
//we show the names of the fields
for ($i = 0; $i <$this-> numcampos (); $i ++) {
I begin "<td> <b>".$this-> nombrecampo ($i)." </b> </td> n";
}
I begin "</tr> n";
//mostrarmos the records
while ($row = mysql_fetch_row ($this-> Consulta_ID)) {
I begin "<tr> n";
for ($i = 0; $i <$this-> numcampos (); $i ++) {
I begin" <td> ".$row [$i]." </td> n";
}
I begin "</tr> n";
}
}
} //end of Clse DB_mysql
?>
Since you will have observed, to create a class we use the judgment class, and also we have created a function with the same name as the class, there is called he to this function a builder and it will be executed whenever we define a variable of this class. Variable of this class is not obligatorina. A builder is not obligatory to create in a class definition.
Another important thing in the classes is the operator->, with that we indicate a variable or method (right part of the operator) of a class (left part of the operator). To allude to the class that we are creating inside his definition, we must use this.
And now let's see an example of the class that we have created, and let's suppose that we have kept the previous code in a called file clase_mysql.inc.php.
<body>
<html>
<? php
require ("clase_mysql.inc.php");
$miconexion = new DB_mysql;
$miconexion-> to tune in ("mydb", "localhost", "nobody", "");
$miconexion-> it consults ("SELECT * FROM agenda");
$miconexion-> verconsulta ();
?>
</body>
</html>
|