Implementar patron singleton
Publicado por caru (27 intervenciones) el 22/02/2014 22:05:37
Bueno el caso es que realize un script Singleton.php, para conexion a la base de datos, pero ahora me piden implementarlo en un sistema que habiamos hecho previamente, en dicho sistema tenia un script de conexion:
Y solo hacia
Pero quiero saber como utilizar es te archivo:
por ejemplo en este archivo:
Probe como ven ahi:
inclui el archivo y llame a la funcion, pero no se si esta bien...
GRACIAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
function conectarDB(){
$conexion=mysql_connect("localhost","root","");
if (!$conexion){
die('Error al conectarse a mysql: ' . mysql_error());
}else{
return $conexion;
}
}
function desconectarDB(){
mysql_close($conexion);
}
?>
Y solo hacia
1
$con= conectarDB()
Pero quiero saber como utilizar es te archivo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
class conectarDB{
static private $instancia=NULL;
private $servidor="localhost";
private $user="root";
private $pass="";
private $bd="sgu";
private $link;
private $stmt;
private $array;
static $_instance;
private function __construct(){
$this->conectar();
}
public static function getInstance(){
if (!(self::$_instance instanceof self)){
self::$_instance=new self();
}
return self::$_instance;
}
private function conectar(){
$this->link=mysql_connect($this->servidor, $this->user, $this->pass);
mysql_select_db($this->bd,$this->link);
}
public function ejecutar($sql){
$this->stmt=mysql_query($sql,$this->link);
return $this->stmt;
}
public function obtener_fila($stmt,$fila){
if ($fila==0){
$this->array=mysql_fetch_array($stmt);
}else{
mysql_data_seek($stmt,$fila);
$this->array=mysql_fetch_array($stmt);
}
return $this->array;
}
public function lastID(){
return mysql_insert_id($this->link);
}
}
?>
por ejemplo en este archivo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
include("../php/modelo/Singleton.php");
$con=conectarDB::getInstance();
function get_user_porid ($id){
if(isset($id)){
$sql="select *
from personas, usuarios
where
personas.idpersona=$id
";
mysql_select_db("sgp",$con);
$resultado=mysql_query($sql,$con) or die('Mi error es: '.mysql_error());
$registro=mysql_fetch_object($resultado);
return $registro;
}
}
if ($_GET['id']!=''){
$persona= get_user_porid($_GET['id']);
}
?>
<html>
<head>
<meta charset="UTF-8"/>
<title>Modificar usuario seleccionado</title>
</head>
<body>
<p align="center"><?php include("../php/header.php"); ?></p>
<form action="includes/modificoPersona.php" method="post" name="form">
<input type="hidden" name="id" value="<?php echo $persona->idpersona ?>" />
<table align="center" width="50%">
<tr>
<td>
<fieldset>
<legend>Relaize los cambios</legend>
<table width="50%" cellpadding="5" border="0">
<tr>
<td><p>Tipo de documento</p></td>
<td><input type="text" name="tipo_documento" value="<?php echo $persona->idTipoDocumento?>"/></td>
</tr>
<tr>
<td><p>Numero</p></td>
<td><input type="text" name="numero" value="<?php echo $persona->numeroDocumento?>"/></td>
</tr>
<tr>
<td><p>Apellido</p></td>
<td><input type="text" name="apellido" value="<?php echo $persona->apellido?>"/></td>
</tr>
<tr>
<td><p>Nombres</p></td>
<td><input type="text" name="nombres" value="<?php echo $persona->nombres?>"/></td>
</tr>
<td><p>Fecha de nacimiento</p></td>
<td><input type="text" name="fechanac" value="<?php echo $persona->fechaNacimiento?>"/></td>
<tr>
<td><p>Sexo</p></td>
<td><input type="text" name="sexo" value="<?php echo $persona->sexo?>"/></td>
</tr>
<tr>
<td><p>Celular</p></td>
<td><input type="text" name="Cel" value="<?php echo $persona->telefonoMovil?>"/></td>
</tr>
<tr>
<td><p>Telefono</p></td>
<td><input type="text" name="Tel" value="<?php echo $persona->telefono?>"/></td>
</tr>
<tr>
<td><p>email</p></td>
<td><input type="text" name="email" value="<?php echo $persona->email?>"/></td>
</tr>
<tr>
<td><p>Domicilio</p></td>
<td><input type="text" name="dom" value="<?php echo $persona->domicilio?>"/></td>
</tr>
<tr>
<td><p>Provincia</p></td>
<td><input type="text" name="prov" value="<?php echo $persona->provincia?>"/></td>
</tr>
<tr>
<td><p>Localidad</p></td>
<td><input type="text" name="loc" value="<?php echo $persona->localidad?>"/></td>
</tr>
<tr>
<td><p>Pais</p></td>
<td><input type="text" name="pais" value="<?php echo $persona->pais?>"/></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Datos de usuario</legend>
<table width="50%" cellpadding="5" border="0">
<tr>
<td><p>User</p></td>
<td><input type="text" name="user" value="<?php echo $persona->username?>"/></td>
</tr>
<tr>
<td><p>Pass</p></td>
<td><input type="text" name="pass" value="<?php echo $persona->password?>"/></td>
</tr>
<tr>
<td><p>Tipo de Usuario</p></td>
<td><input type="text" name="tipouser" value="<?php echo $persona->idTipoUsuario?>"/></td>
</tr>
<tr>
<td><input type="submit" name="bt_enviar" value="Listo"/></td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</form>
</body>
</html>
Probe como ven ahi:
1
2
include("../php/modelo/Singleton.php");
$con=conectarDB::getInstance();
GRACIAS
Valora esta pregunta


0