Doubt with Mysqli and PHP OO

Asked

Viewed 525 times

1

I have the following code snippet:

public function registerUser($link, $nome, $sobrenome, $apelido) {
    $sql = "INSERT INTO estudo_oo (id, nome, sobrenome, apelido) VALUES ('', '$nome', '$sobrenome', '$apelido') ";
    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }
}

And my question is: Would I have to ALWAYS pass the variables by parameter there in the function? Because like, if I had a form with multiple fields for example, I would actually have to pass all variables there?

I did some tests and tried to save the form without passing the variables by parameter, but I was not successful, then I came up with this question.

3 answers

2

More directly answering your question:

I would have to pass variables by parameter ALWAYS there in the function?

At some point you have to pass the arguments, either by defining class properties, or as method arguments. Which of the two ways (or both) you will use depends only on your decisions for the project.

1

In object orientation we have class properties, which are nothing more than variables within the scope of the class.

The mysqli allows both procedural method (which is what you are using) and object-oriented method.

I won’t go into more detail, but here is a simple example of how to use the properties:

<?php

class Usuario {

    // A propriedade com uma conexão
    protected $dbConnection;

    // Método construtor para passagem do parâmetro (nesse caso sua conexão com banco)
    public function __construct(mysqli $dbConnection)
    {
        // Injeta a conexão criada na classe
        $this-> dbConnection = $dbConnection;
    }

    public function registerUser($nome, $sobrenome, $apelido) 
    {
        // Recomendo o uso de prepared statments para inserir os parâmetros
        $sql = "INSERT INTO estudo_oo (id, nome, sobrenome, apelido) VALUES ('', '$nome', '$sobrenome', '$apelido') ";

        // Procure usar Exceptions para tratamento de erros
        try {
            $this->query($sql);
        } catch (mysqli_sql_exception $e) {
            return $e->message;
        }

        return true;

    }
}

// Como usar:

$link = new mysqli('localhost', 'user', 'pass');

$usuario = new Usuario($link);
$usuario->registerUser('João', 'Lucas', 'Jão');

These were just a few tips on how to implement. Other suggestions are use PDO or instead of Mysqli and read a little more of basics in modern PHP development.

1

You can set default values for parameters you don’t want to always pass:

public function registerUser($link, $nome, $sobrenome=null, $apelido=null) {

}

Note: as you are using mysqli, strongly recommend using Prepared statements instead of concatenating the variables directly into the query.

  • Hit a question: passing a null variable in a Prepared statement will insert the null value in the bank or a string empty?

  • 1

    If you use bind_param and pass null, the value NULL will be inserted.

Browser other questions tagged

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