-3
Well, I’d like to know how I return a variable out of the object by running a method in PHP.
Follows the code:
Connection.php
<?php
class Connection {
protected $db_host;
protected $db_user;
protected $db_pass;
protected $database;
function __construct($db_host, $db_user, $db_pass, $database){
$this -> db_host = $db_host;
$this -> db_user = $db_user;
$this -> db_pass = $db_pass;
$this -> database = $database;
}
function getDB_host(){
return $this -> db_host;
}
function getDB_user(){
return $this -> db_user;
}
function getDB_pass(){
return $this -> db_pass;
}
function getDatabase(){
return $this -> database;
}
function setDB_host($db_host){
$this -> db_host = $db_host;
}
function setDB_user($db_user){
$this -> db_user = $db_user;
}
function setDB_pass($db_pass){
$this -> db_pass = $db_pass;
}
function setDatabase($database){
$this -> database = $database;
}
function connect(){
@$connection = mysqli_connect($this -> getDB_host(), $this -> getDB_user(), $this -> getDB_pass(), $this -> getDatabase());
if(mysqli_connect_errno($connection)){
echo "Erro ao fazer conexão com MySQL";
die();
}
// eu quero fazer o retorno da variáel '$connection' para fora do objeto
}
}
?>
index php.
<?php
require_once 'connection.php';
$newConnection = new Connection('127.0.0.1' /* 'Servidor do MySQL' */, 'root' /* 'Usuário do MySQL */, '*****' /* 'Senha do MySQL*/, 'database' /* 'Database do MySQL' */);
$newConnection -> connect();
// eu quero extrair aqui o valor da variável '$connection' do método connect()
?>
Thanks, I help a lot.
– user106463