2020-03-08 04:37:19 +01:00
|
|
|
<?php
|
2020-03-17 18:46:16 +01:00
|
|
|
/*
|
2021-10-22 05:34:49 +02:00
|
|
|
* DuckBrain - Microframework
|
|
|
|
*
|
2022-03-17 22:22:04 +01:00
|
|
|
* Clase diseñada para crear y devolver una única instancia mysqli (database).
|
2021-10-22 05:34:49 +02:00
|
|
|
* Depende de manera forzada de que estén definidas las constantes:
|
|
|
|
* dbhost, dbname, dbpass y dbuser
|
|
|
|
*
|
|
|
|
* Autor: KJ
|
|
|
|
* Web: https://kj2.me
|
|
|
|
* Licencia: MIT
|
|
|
|
*/
|
2020-03-17 18:46:16 +01:00
|
|
|
|
2020-03-08 04:37:19 +01:00
|
|
|
namespace Libs;
|
2022-03-17 22:22:04 +01:00
|
|
|
use mysqli;
|
2020-03-08 04:37:19 +01:00
|
|
|
|
2022-03-17 22:22:04 +01:00
|
|
|
class Database extends \mysqli {
|
2021-10-22 05:34:49 +02:00
|
|
|
static private $db;
|
|
|
|
|
|
|
|
private function __construct() {}
|
|
|
|
|
2022-03-17 22:22:04 +01:00
|
|
|
/**
|
|
|
|
* Devuelve una instancia homogénea (singlenton) a la base de datos.
|
|
|
|
*
|
|
|
|
* @return mysqli
|
|
|
|
*/
|
|
|
|
static public function getConnection() : mysqli {
|
2021-10-22 05:34:49 +02:00
|
|
|
if (!isset(self::$db)) {
|
2022-03-17 22:22:04 +01:00
|
|
|
self::$db = new mysqli(dbhost, dbuser, dbpass, dbname);
|
2021-10-22 05:34:49 +02:00
|
|
|
if (self::$db->connect_errno) {
|
|
|
|
echo '<style>body{white-space: pre-line;}</style>';
|
|
|
|
throw new \Exception('No se ha podido conectar a la base de datos.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$db;
|
2020-03-08 04:37:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|