PHP with DAO + mysqli

Asked

Viewed 759 times

5

DAO necessarily needs to be a class? Or I could create methods within a file. php without encapsulation of a class?

If not possible, what is the name of the classless DAO (name of some pattern equal or similar)?

Is it advisable to use DAO (with or without class) with procedural mysqli? Why? What are the advantages and disadvantages?

  • It’s a matter of style, you’ve tried both to see which one looks better?

  • From what I read, you use object (class) is almost practically organization. Then you have all your methods in a separate file, or even in a single one, I believe you would have no problems, thinking as an example "phpmyadmin". Let’s hope the knowledgeable people respond.

  • Good way to solve a test question :)

1 answer

7


"DAO necessarily needs to be a class? Or I could create methods within a file. php without encapsulation of a class?"

There is no DAO without classes. That doesn’t mean you can’t do something similar, but it won’t be DAO. DAO has very specific responsibilities:

  • provides an interface that abstracts data access;
  • reads and writes from the data source (database, file, memory, etc.); and
  • encapsulates the access to the data, so that the other classes do not need to know about this.

And this is more evident in oracle documentation. That says so:

Use a Data Access Object (DAO) to abstract and encapsulate the whole access to the data source. DAO manages the connection to the data source to obtain and store data.

You will not be able to carry out these responsibilities without classes.

You have to ask yourself:

  • In my application I have several entities that will be related to each other? Or each entity will be treated unilaterally.
  • HOW MUCH THIS APPLICATION CAN GROW?
  • How many tables will I access at the same time?
  • How many banks will I access?
  • And if you change the database or the server?

Instead of thinking about using DAO, you can create a generic CRUD.

AN EXAMPLE:

Let’s assume that your entire application will be structured, that is, without object instances and each file(script) will be executed one at a time when it is called by the client and this application will not grow because it is very simple....

A hotsite.

crud.php

function inserir($tabela, $values = array()){

    $con = conexao();
    $sql = "INSERT INTO ".$tabela;
    $valoresDB = "(";
    $valoresBlind = "("
    foreach($values as $key => $valor){
        $valoresDB .= $key.","
        $valoresBlind .= "?,"
    }
    $sql .= " ".substr($valoresDB, 0, -1).") ".substr($valoresBlind, 0, -1).") ";
    $stm = mysqli_stmt_prepare($con, $sql);
    foreach($values as $valor){
        mysqli_stmt_bind_param($stm, "s", $valor); 
    }
    if(mysqli_stmt_execute($stm)){
        return true;
    }
    return false;
}

function alterar($tabela, $values = array(), $where = array()){
    // altera um registro
}

function deletar($tabela, $id, $where){
    // deletar
}

function listar($tabela, $id, $where){
    // listar 1 ou mais registros
}

In this case, this CRUD could serve better than a DAO, as you would write less code, facilitate its maintenance, development, etc.


If not possible, what is the name of the classless DAO (name of some pattern equal or similar)?

CRUD. = D

There is no specific CRUD standard for structured programming.

"If it’s not possible"...

In my opinion It is POSSIBLE for you to do something similar to DAO. You can create files with the name ProdutoDAO.php and create CRUD functions. But this will not be DAO.

It is also important to know various types of patterns, or even create your own. Why not?

According to the characteristics of Design Pattern

  1. They must have a NAME that describes the problem, the solutions and consequences. A name allows defining the vocabulary to be used by designers and developers at a higher level of abstraction.
  2. Every pattern should report clearly to which (is) PROBLEM(s) it should be applied, ie what are the problems that when inserted in a certain context the standard will resolve it.Some may require preconditions.
  3. Solution describes the elements that make up the project, its relationships, responsibilities and collaborations. A pattern should be a concrete SOLUTION, it should be expressed in the form of template (algorithm) which however can be applied in ways different.
  4. Every standard should report what are its CONSEQUENCES so that the alternative solution of projects and for the understanding of the benefits of applying the project.

Is it advisable to use DAO (with or without class) with procedural mysqli? Why? What are the advantages and disadvantages?

The procedural or object-oriented mysqli will depend on you. In my view php kept the procedural line so that the "old Schools" did not feel so much the difference and/or to facilitate the maintenance of old applications. I see that for myself manual that says so:

Users migrating from the old mysql extension may prefer the interface procedural. The procedural interface is similar to the old extension mysql. In many cases, function names differ only by prefix. Some mysqli functions take a connection identifier as its first argument, while the corresponding functions in the old mysql interface take it as an optional last argument.

Free translation =p

Using mysqli procedural or object-oriented does not change anything. If there is any gain in performance it is not relevant. Maybe the only difference is that with OOP you get something like this:

class Banco extends mysqli{..}

Make it the way it’s most comfortable for you.

EDIT

After researching a little, I found no disadvantages or advantages in using mysqli in procedural form or OO. It may be more relevant to know if you should use Pro Red statements or not for performance gain. You have a publishing from @Maniero on this. I even found it interesting to highlight:

In addition it is possible to have some performance gain because it (Prepared statements) can be compiled and curly. But depending on the usage pattern will not be other than an unprepared consultation. In other cases this is possible won’t even be enjoyed. Remember that the preparation lasts only during the session which is usually short.

In other words, even in this issue, this is not TAO relevant and it seems to me that it is not consecrated. I read a few articles, not very important, that advocate the use of mysqli OO. Some say that PHP is increasingly "Object Oriented" and that the sooner you get used to programming like this, the better, because "will php depress the procedural form"...

It’s easier to consult the "Mother Dinah"!

But these issues are not directly related to the DAO. You can use any form of access to the database provided you make use of the DAO standard to abstract and encapsulate all access to the data source, extract and/or store information, etc.

An example use of DAO with @Virgilionovic procedural mysqli

I also found it interesting to reply from @utluiz on DAO, is well completed, worth taking a look.

  • Thanks helped a lot, I just do not accept the answer because I wanted to know more about if it is advisable, the advantages and disadvantages of using DAO + mysqli procedural

  • @Guilhermecostamilam really I don’t know how to advise this for specific use in DAO. But in my view this does not change in terms of performance. The difference may be in being able to extend mysqli and use it object-oriented. The same thing you can do with PDO. I THINK (I’m not sure) that it’s more a matter of taste anyway.

  • @Guilhermecostamilam but still. It hopes to come other answers. It may be until I’m not quite sure, since there was not much acceptance.

Browser other questions tagged

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