Using a class function within another PHP class

Asked

Viewed 23,592 times

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;

    }
}

2 answers

10


An option would be to instantiate a new object within the class, for example:

class MinhaClasseDois
{
    public function minhaFuncaoDaClasseDois()
    {
        $objeto = new MinhaClasseUm();
        $banana = $objeto->minhaFuncaoDaClasseUm();
    }
}

Without creating a new object, one could use the global PHP to reference an existing instance, but not a suitable solution for code reuse:

$instanciaDaClasseUm = new MinhaClasseUm();

class MinhaClasseDois
{
    public function minhaFuncaoDaClasseDois()
    {
        global $instanciaDaClasseUm;
        $banana = $instanciaDaClasseUm->minhaFuncaoDaClasseUm();
    }
}

As I commented, a better option for your particular case, would be to pass by parameter, which need not change almost anything in its original class, requiring only a few adjustments in fnAcao:

class Acao{
   public function fnAcao($db) {
      $query = "SELECT * FROM tabela";
      $result = $db->ExecutarSQL($query);
      return $result;
   }
}

// Usando a passagem por parâmetro:
$db = new DB( 'Usuario', 'senha123', 'minhaBaseDeDados', 'exemplo.com' );
$db->ConectarBanco(); //Esta linha pode ir para a ação se desejar
$acao = new Acao();
$resultado = $acao->fnAcao($db); //Passando por parâmetro
$db->FecharBanco(); //Esta linha tambem pode ir para a ação se desejar
  • 2

    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.

  • 2

    It also has the option to use static methods. But the question code would need to be completely rewritten.

  • Global? Seriously? I have three words for you: Composition, Aggregation and Injection. ;)

4

I would Usuaria dependency injection, in the class that will receive the seat class, in the constructor, lowering coupling.

class DBInterface {
    public function ConectarBanco();
    public function FecharBanco();
    public function ExecutarSQL($sql);
    public function Consultar($dados);
}

class DB implements DBInterface{
    private $user;
    private $password;
    private $database;
    private $host;  
    public function __construct($user, $password, $database, $host) {
        $this->user = $user;
        $this->password = $password;
        $this->database = $database;
        $this->host = $host;
        $this->ConectarBanco();
    }
    public 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
{
    //INJEÇÃO PELO CONSTRUTOR DA CLASSE
    private $db;
    public function __construct(DBInterface $db){
        $this->db = $db;
    }
    public function fnAcao()
    {
        $query = "SELECT * FROM tabela";
        $result = $this->db->ConsultarSQL($query);
        return $result;
    }
}

Using:

$db        = new DB();
$acao      = new Acao($db); //aqui com injeção
$resultado = $acao->fnAcao();

Although it would indicate a better model for you with a DAL layer, POCO/DTO and Connection each with your responsibility.

Reference:

  • Injection of FTW Dependency! However, in my opinion this... "technique", in the absence of a better term, is closely related to object typing, in this case, with the above example code, without Interfaces, the builder of Action would receive an object of the type DB

  • I agree with Interface, but, I myself reported I would indicate a model better seen in what the asked presented, and just for me to know what is FTW? @Brunoaugusto

  • @Harrypotter FTW = For The Win

  • @Bacco I got it, vlw...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.