3
I have two classes, which is the correct way to call a function of another class, the way below returns error
class DB {
public function __construct($user, $password, $database, $host) {
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->host = $host;
$this->ConectarBanco();
}
protected function ConectarBanco() {
$this->mysqli = new mysqli($this->user, $this->password, $this->database, $this->host);
if (mysqli_connect_errno()) {
die('Não foi possível conectar-se ao banco de dados');
exit();
}
}
public function FecharBanco() {
$this->mysqli->close();
}
public function ExecutarSQL($sql) {
$this->result = $mysqli->query($sql);
return $this->result;
}
public function Consultar($dados) {
$this->newRow = array();
while($row = $mysqli->fetch_array($dados)) {
array_push($this->newRow,$this->row);
}
return $this->newRow;
}
}
class Acao{
public function fnAcao(){
$query = "SELECT * FROM tabela";
$result = DB::ConsultarSQL($query);
return $result;
}
}
In the second case, beware, because it can complicate the reuse of the code. In a practical case, it may be better to pass the instance of the class one as parameter.
– Bacco
It also has the option to use static methods. But the question code would need to be completely rewritten.
– bfavaretto
Global? Seriously? I have three words for you: Composition, Aggregation and Injection. ;)
– Bruno Augusto