Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/02/2014, 11:26
Avatar de giuli956
giuli956
 
Fecha de Ingreso: noviembre-2012
Mensajes: 149
Antigüedad: 11 años, 5 meses
Puntos: 1
Implementar patron singleton

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:

Código PHP:
Ver original
  1. <?php
  2.     function conectarDB(){
  3.         $conexion=mysql_connect("localhost","root","");
  4.         if (!$conexion){
  5.             die('Error al conectarse a mysql: ' . mysql_error());  
  6.         }else{
  7.             return $conexion;
  8.         }
  9.     }
  10.     function desconectarDB(){
  11.         mysql_close($conexion);        
  12.     }
  13.  
  14. ?>

Y solo hacia

Código PHP:
Ver original
  1. $con= conectarDB()

Pero quiero saber como utilizar es te archivo:

Código PHP:
Ver original
  1. <?php
  2.  
  3. class conectarDB{
  4.     static private $instancia=NULL;
  5.     private $servidor="localhost";
  6.     private $user="root";
  7.     private $pass="";
  8.     private $bd="sgu";
  9.     private $link;
  10.     private $stmt;
  11.     private $array;
  12.    
  13.     static $_instance;
  14.    
  15.     private function __construct(){
  16.         $this->conectar();
  17.     }
  18.     public static function getInstance(){
  19.       if (!(self::$_instance instanceof self)){
  20.          self::$_instance=new self();
  21.       }
  22.       return self::$_instance;
  23.    }
  24.    private function conectar(){
  25.       $this->link=mysql_connect($this->servidor, $this->user, $this->pass);
  26.       mysql_select_db($this->bd,$this->link);
  27. }
  28.     public function ejecutar($sql){
  29.       $this->stmt=mysql_query($sql,$this->link);
  30.       return $this->stmt;
  31.    }
  32.  
  33.    public function obtener_fila($stmt,$fila){
  34.       if ($fila==0){
  35.          $this->array=mysql_fetch_array($stmt);
  36.       }else{
  37.          mysql_data_seek($stmt,$fila);
  38.          $this->array=mysql_fetch_array($stmt);
  39.       }
  40.       return $this->array;
  41.    }
  42.  
  43.  
  44.    public function lastID(){
  45.       return mysql_insert_id($this->link);
  46.    }
  47. }
  48. ?>

por ejemplo en este archivo:

Código PHP:
Ver original
  1. <?php
  2. include("../php/modelo/Singleton.php");
  3. $con=conectarDB::getInstance();
  4.      function get_user_porid ($id){
  5.     if(isset($id)){
  6.         $sql="select *
  7.        from personas, usuarios
  8.        where
  9.        personas.idpersona=$id
  10.        ";  
  11.        
  12.         mysql_select_db("sgp",$con);
  13.         $resultado=mysql_query($sql,$con) or die('Mi error es: '.mysql_error());
  14.         $registro=mysql_fetch_object($resultado);
  15.      
  16.         return $registro;
  17. }
  18. }
  19. if ($_GET['id']!=''){
  20. $persona= get_user_porid($_GET['id']);
  21.  
  22. }
  23.  
  24.  
  25.  
  26.  
  27.    
  28. ?>
  29. <html>
  30.     <head>
  31.         <meta charset="UTF-8"/>
  32.         <title>Modificar usuario seleccionado</title>
  33.     </head>
  34.     <body>
  35.         <p align="center"><?php include("../php/header.php"); ?></p>
  36.         <form action="includes/modificoPersona.php" method="post" name="form">
  37.         <input type="hidden" name="id" value="<?php echo $persona->idpersona ?>" />
  38.             <table align="center" width="50%">
  39.             <tr>
  40.             <td>
  41.                 <fieldset>
  42.                     <legend>Relaize los cambios</legend>
  43.                     <table width="50%" cellpadding="5" border="0">
  44.                        
  45.                         <tr>                
  46.                             <td><p>Tipo de documento</p></td>
  47.                             <td><input type="text" name="tipo_documento" value="<?php echo $persona->idTipoDocumento?>"/></td>
  48.                         </tr>
  49.                         <tr>
  50.                             <td><p>Numero</p></td>
  51.                             <td><input type="text" name="numero" value="<?php echo $persona->numeroDocumento?>"/></td>
  52.                         </tr>
  53.                         <tr>
  54.                             <td><p>Apellido</p></td>
  55.                             <td><input type="text" name="apellido" value="<?php echo $persona->apellido?>"/></td>
  56.                         </tr>
  57.                         <tr>                            
  58.                             <td><p>Nombres</p></td>
  59.                             <td><input type="text" name="nombres" value="<?php echo $persona->nombres?>"/></td>
  60.                         </tr>                            
  61.                             <td><p>Fecha de nacimiento</p></td>
  62.                             <td><input type="text" name="fechanac" value="<?php echo $persona->fechaNacimiento?>"/></td>
  63.                         <tr>
  64.                             <td><p>Sexo</p></td>
  65.                             <td><input type="text" name="sexo" value="<?php echo $persona->sexo?>"/></td>
  66.                         </tr>
  67.                         <tr>    
  68.                             <td><p>Celular</p></td>
  69.                             <td><input type="text" name="Cel" value="<?php echo $persona->telefonoMovil?>"/></td>
  70.                         </tr>
  71.                         <tr>
  72.                             <td><p>Telefono</p></td>
  73.                             <td><input type="text" name="Tel" value="<?php echo $persona->telefono?>"/></td>
  74.                         </tr>
  75.                         <tr>
  76.                             <td><p>email</p></td>
  77.                             <td><input type="text" name="email" value="<?php echo $persona->email?>"/></td>
  78.                         </tr>
  79.                         <tr>
  80.                             <td><p>Domicilio</p></td>
  81.                             <td><input type="text" name="dom" value="<?php echo $persona->domicilio?>"/></td>
  82.                         </tr>
  83.                         <tr>
  84.                             <td><p>Provincia</p></td>
  85.                             <td><input type="text" name="prov" value="<?php echo $persona->provincia?>"/></td>
  86.                         </tr>
  87.                         <tr>
  88.                             <td><p>Localidad</p></td>
  89.                             <td><input type="text" name="loc" value="<?php echo $persona->localidad?>"/></td>
  90.                         </tr>
  91.                         <tr>
  92.                             <td><p>Pais</p></td>
  93.                             <td><input type="text" name="pais" value="<?php echo $persona->pais?>"/></td>
  94.                         </tr>
  95.                    </table>
  96.                 </fieldset>
  97.                      <fieldset>
  98.                     <legend>Datos de usuario</legend>
  99.                     <table width="50%" cellpadding="5" border="0">
  100.                         <tr>
  101.                             <td><p>User</p></td>
  102.                             <td><input type="text" name="user" value="<?php echo $persona->username?>"/></td>
  103.                         </tr>
  104.                         <tr>
  105.                             <td><p>Pass</p></td>
  106.                             <td><input type="text" name="pass" value="<?php echo $persona->password?>"/></td>
  107.                         </tr>
  108.                         <tr>
  109.                             <td><p>Tipo de Usuario</p></td>
  110.                             <td><input type="text" name="tipouser" value="<?php echo $persona->idTipoUsuario?>"/></td>
  111.                         </tr>
  112.                        
  113.                         <tr>
  114.                             <td><input type="submit" name="bt_enviar" value="Listo"/></td>
  115.                         </tr>
  116.                    </table>
  117.  
  118.                     </fieldset>
  119.              
  120.                     </td>
  121.                     </tr>
  122.                 </table>
  123.        
  124.         </form>
  125.     </body>
  126.    
  127. </html>

Lo que hice fue:

Código PHP:
Ver original
  1. include("../php/modelo/Singleton.php");
  2. $con=new conectarDB();
  3. $con->getInstance();

Obtengo el siguiente error:

Fatal error: Call to private conectarDB::__construct() from invalid context
Probe hacer publica el metodo constructor, pero igual da otros errores, no sera que tengo que pasar toda la aplicacion a objetos?