Saludos.
Estoy aprendiendo php y estoy tratando de llamar una clase de conexiona la db desde otra clase
Este es la clase conexion que utiliza singleton
class.database.php
Código:
<?php
/*
* Mysql database class - only one connection alowed
*/
class Database {
private $_connection;
private static $_instance; //The single instance
private $_host = "HOSTt";
private $_username = "USERNAME";
private $_password = "PASSWORd";
private $_database = "DATABASE";
/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
E_USER_ERROR);
}
}
// Magic method clone is empty to prevent duplication of connection
private function __clone() { }
// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
}
?
Este es mi codigo para llamar y me arroja error
lotes.php[
Código:
<?php
include_once './class.database.php';
class lotes {
public $db;
public function __construct() {
$this->db = Database::getConnection();
}
function listarNombres() {
$sql = "SELECT * FROM nombres";
$result = $this->db->query($sql);
while ($row = $result->fetch_array()) {
echo '<option value="' . $row['id'] . '">' . $row['nombre'] . '</option>';
}
}
}
$test = new lotes();
$test->listarNombres();
Error que arroja:
Strict standards: Non-static method Database::getConnection() should not be called statically, assuming $this from incompatible context