OO and non-OO usage of Mysqli I'm starting to learn the object oriented aspect of PHP. In this regard I made a small "exercise" of sorts "translating" one of my PHP functions into OO mode of PHP. I'm asking if there are there any recommendations you can give me concerning my predicament.

Example of non-OO PHP:

&lt;?php
function pristup($servername, $username, $password, $dbname, $sql){

    $conn=new mysqli($servername, $username, $password, $dbname);

    if($conn-&gt;connect_error){

        die("Neuspela konekcija: ".$conn-&gt;connect_error);

    }

    $result = $conn-&gt;query($sql);
    if ($result == TRUE) {
    //echo "Uspela konekcija";
} else {
    echo "Neuspešno izvršavanje upita: " . $conn-&gt;error;
}
    return $result;

    $conn-&gt;close();

}

?&gt;


Example of this done in an object oriented manner:

&lt;?php

class konekcija{

    private $servername;
    private $username;
    private $password;
    private $dbname;
    private $sql;

    //Setter functions
    public function setVal($par1, $par2, $par3, $par4, $par5){ //Setovanje vrednosti za upit.
    $this-&gt;servername = $par1;
    $this-&gt;username = $par2;
    $this-&gt;password = $par3;
    $this-&gt;dbname = $par4;
    $this-&gt;sql = $par5; 

}

    //Getter functions
    public function getServername() { 
        return $this-&gt;servername;
    }

    public function getUsername() { 
        return $this-&gt;username;
    }

    public function getPassword() { 
        return $this-&gt;password;
    }

    public function getDBname() { 
        return $this-&gt;dbname;
    }

    public function getSQL() { 
        return $this-&gt;sql;
    }

    //Function that executes query.
    public function pristup($server_name, $user_name, $pass_word, $db_name, $sql_query){

        $conn=new mysqli($server_name, $user_name, $pass_word, $db_name);

        if($conn-&gt;connect_error){

            die("Neuspela konekcija: ".$conn-&gt;connect_error);

        }

        $result = $conn-&gt;query($sql_query);

        if ($result == TRUE){

        echo "Uspela konekcija";

        } 
        else{

                echo "Neuspešno izvršavanje upita: " . $conn-&gt;error;

            }

        return $this-&gt;$result;

        $conn-&gt;close();

    }

}


$kon = new konekcija(); //Creation of an object.

//Setting values.
$kon -&gt; setVal("localhost", "root", "", "test", "CREATE TABLE example(
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
            )");

//Getting values and inserting them into class method.
$kon -&gt; pristup($kon-&gt;getServername(), $kon-&gt;getUsername(), $kon-&gt;getPassword(), $kon-&gt;getDBname(), $kon-&gt;getSQL());

?&gt;


This works, and I'm asking if this code is any good. Is there a better/more established way of doing this?
Most of the code in this class just makes no sense and written only because it has been shown in some tutorial as an example for some other class.
In case of a database connection class, there is no use for the database credentials' getters. You never need a database host anywhere outside of the database class. Hence, getServername() method will never be used. Neither such class variables are actually needed as you only need them in the constructor. And of course,  there shouldn't be a setVal() method whic is way too localised to be a part of the database class.
From the above, it can be clearly seen that there is no useful functionality in this class, and for a reason: mysqli is already a class, and not the worst one.
In your case, it would be much, much better to learn how to use vanilla mysqli first.
OO usage of mysqli is simple. All you need to know is the main rule of the database interaction in PHP:
A database connection must be created strictly once!
Therefore, you have to create the mysqli object first, and then use this instance for all the database interactions in your script. The simplest code for this would take just three lines:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // enable error reportig
$conn= new mysqli($host, $user, $pass, $db, $port); // the actual connection
$conn-&gt;set_charset($charset); // set connection charset

After this, you can use the instance of mysqli class to run SQL queries OO style:
$sql = &quot;CREATE TABLE example(
        id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL,
        email VARCHAR(50),
        reg_date TIMESTAMP
        )&quot;;
$conn-&gt;query($sql);

And it would be the perfect example of using mysqli OO style.
Other OO usage  examples of mysqli can be found in this answer