How to Connect to the Bank Reading INI File (Function and Class)

0

How can I connect to the database (PHP in 3 Layers [Façade / Repository / Bank]) by reading the information from an .ini file and assigning those values to a function inside a class?

Banco.php

class Banco {
  var $server;
  var $bd;
  var $user;
  var $password;
  var $erro;
  var $result;
  var $conexao;

  function Banco ($user="usuario",$pass="senha",$db="banco",$srv="servidor") {
    if(!$this -> SetConexao($srv,$user,$pass)) {
      $this -> SetErro("Erro de Conexão - ".mssql_get_last_message($this -> GetConexao()));
      mssql_close();
      return false;
    }
    if(!$this -> SetBd($db)) {
      $this -> SetErro("Banco não Encontrado - ".mssql_get_last_message($this -> GetConexao()));
      mssql_close();
      return false;
    }
  }
.......... mais código .........
}

.INI file

[BANCO]
user = usuario
pass = senha
db = banco
srv = servidor

Reading .INI File

$ini = parse_ini_file('config.ini', true);
$cUser = $ini['BANCO']['user'];
$cPass = $ini['BANCO']['pass'];
$cDb = $ini['BANCO']['db'];
$cSrv = $ini['BANCO']['srv'];

Class .... {

blabla.....

function Banco ($user=$cUser,$pass=$cPass,$db=$cDb,$srv=$cSrv) { <-- Erro ao conectar ao banco

Would anyone have any idea how I could make this kind of connection without having to undo the connection layers in php?

    
asked by anonymous 22.03.2016 / 01:29

1 answer

1

You are mixing the definition of the class with the instantiation of the object. = is not passed so directly. You need to do something like this:

<?php

// Aqui vem a definação da classe
class Banco { 
// ...
}

// Aqui a instanciação
$ini = parse_ini_file('config.ini', true);
$cUser = $ini['BANCO']['user'];
$cPass = $ini['BANCO']['pass'];
$cDb = $ini['BANCO']['db'];
$cSrv = $ini['BANCO']['srv'];


$db = new Banco();
$db->Banco($cUser, $cPass, $cDb, $cSrv);
    
22.03.2016 / 03:04