Return value from a PHP method

Asked

Viewed 53 times

-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()
?>

1 answer

1


You can return the value of the connection and make queries from it. I’ll give you an example in the code below.

<?php

class Database {
  private $hostname;
  private $username;
  private $password;
  private $basename;

  public function __construct(String $hostname, String $username, String $password, String $basename) {
    $this->hostname = $hostname;
    $this->username = $username;
    $this->password = $password;
    $this->basename = $basename;
  }

  public function connection() {
    return mysqli_connect(
      $this->hostname,
      $this->username,
      $this->password,
      $this->basename
    );
  }
}

$database = new Database("localhost", "root", "ascent", "db");
$connection = $database->connection();

var_dump($connection);

In the above code I create a class with name Connection where it is responsible for receiving the values of the connection in your constructor and the values are private the class, and you leave the public connection method to use outside the class, see more in the link: https://www.php.net/manual/en/language.oop5.visibility.php

  • Thanks, I help a lot.

Browser other questions tagged

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