How to reuse the connection in the subclass?

4

Colleagues, I have a connection class:

class Conecta
{
    private $Servidor = '127.0.0.1';
    private $Usuario = 'root';
    private $Senha = 'senha';
    private $Banco = 'banco';

    // Protegido
    protected $Conecta;

    // Faz a conexão
    public function conectar(){

$this->Conecta = mysqli_connect($this->Servidor,$this->Usuario,$this->Senha,$this->Banco);
}

And I have a class of methods:

    class Metodos extends Conecta
    {
       // Primeiro método
      public functionPrimeiro(){
           $conectar = new Conecta();
            $conectar->conectar();

    $sql = mysqli_query($conectar->conectar(), "MINHA QUERY");
    }

    // Segundo método
      public functionSegunda(){
           $conectar = new Conecta();
            $conectar->conectar();

    $sql = mysqli_query($conectar->conectar(), "MINHA QUERY");
    }
}

Note that within each method I use:

   $conectar = new Conecta();
    $conectar->conectar();

Would there be any way I could not use within each method, or could it just be that way?

    
asked by anonymous 09.11.2015 / 02:23

3 answers

4

Modify the "Connect" class to:

class Conecta
{
    private $Servidor = '127.0.0.1';
    private $Usuario = 'root';
    private $Senha = 'senha';
    private $Banco = 'banco';

    // Protegido
    protected $Conecta;

    // Realiza a conexão no construtor 
    public function __construct()
    {
        $this->Conecta = mysqli_connect($this->Servidor,$this->Usuario,$this->Senha,$this->Banco);
    }
}

To use within your "Methods" class, just do:

class Metodos extends Conecta
{
    // Primeiro método
    public functionPrimeiro()
    {
        $result = mysqli_query($this->Conecta, "MINHA QUERY");
    }

    // Segundo método
    public functionPrimeiro()
    {
        $result = mysqli_query($this->Conecta, "MINHA QUERY");
    }
}

Although it is a functional solution, I recommend that you accept the @bfavaretto county, if you want to unbind it, use PDO!

Hug.

    
09.11.2015 / 03:46
6

You can decouple this code and eliminate inheritance. Start with a connection class that opens the connection to the database as soon as it is instantiated:

class Conexao {

    // (...)

    // Tudo o que tinha na sua classe Conecta,
    // + o construtor abaixo
    function __construct() {
        $this->conectar();
    }

    // Se tem coisa específica do mysqli aqui,
    // pode ser interessante abstrair pelo menos
    // um método genérico de query. A ideia é
    // centralizar aqui tudo que lida direto com mysqli.
    public function query($sql) {
        $qry = mysqli_query($this->Conecta, $sql);
        return mysqli_fetch_all($qry, MYSQLI_ASSOC);
    }

}

In the method class, you inject the connection, also using the constructor:

class Dados {

    // Conexão guardada aqui
    protected $conexao;

    // Construtor: guarda a conexão
    function __construct($conexao) {
        $this->conexao = $conexao;
    }

    // Um método qualquer que usa o BD
    public function pegaDados() {
        return $this->conexao->query("MINHA QUERY");
    }
}

And you use it like this:

$bd = new Dados(new Conexao());
var_dump($bd->pegaDados());
    
09.11.2015 / 03:02
0

As the Connect class is being inherited by the Methods class, you can do this by using parent which is a reference to the parent class in an inheritance relationship:

parent::conectar();

How would you look:

public functionSegunda(){  
  $sql = mysqli_query(parent::conectar(), "MINHA QUERY");
}
    
09.11.2015 / 02:33