Hola amigos capos del foro. Tengo 2 clases que se conectan a diferentes BD y funcionan bien. El tema es que necesito ayuda de la manera correcta de hacer una clase de conexión (incluir) para ambas y así optimizar los códigos.
Puede ser que me haga falta retocar la función __contruct, o los accesos para llegar al éxito.
1era clase:
Código PHP:
class Helper {
//Propiedades
private static $host = 'localhost';
private static $user = 'root';
private static $pass = '';
private static $dbname = 'menu_url';
private static $dbh;
private static $error;
private static $router;
public static function construct(){
// Set DSN -- Donde se conecta
$dsn = 'mysql:host=' . self::$host . ';dbname=' . self::$dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace, crear el objeto de acceso a base de datos
try{
self::$dbh = new PDO($dsn, self::$user, self::$pass, $options);
//funcion para crear el menu desde la base de datos
self::setRouter();
}
// Catch any errors
catch(PDOException $e){
self::$error = $e->getMessage();
}
}
public static function setRouter() {//Le cargo a 'router' los valores de inicialización
.....
}
self::$router = $array;
}
public static function getColumn($file = null ) {
......
}
public static function getContent($file = null) {
.....
}
}
2nda clase:
Código PHP:
class MenuCategoria {
//Propiedades
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $dbname = 'menu_categorias';
private $dbh;
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function display_children($id) {
......
}
}
//Instanceo Helper
$db = new MenuCategoria();
$db->display_children(2);