Project Improvement in PHP

Asked

Viewed 215 times

1

I’m having a question about the default php project. I have a class DAO which has a selection method, well what happens is that I had to put in this method some HTML code stylized with bootstrap, what I did this correct?

class CategoriaDao {
public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    $consulta = $conexao->conectar()->query($sql);     
    while ($sqlSeleciona = $consulta->fetch(PDO::FETCH_OBJ)){       
        echo "<td>" . $sqlSeleciona->descricao . "</td>";
        echo '<td><a href="index.php?action=update&id='.$sqlSeleciona->id_categoria_pagina.'" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-edit"></span> Editar</a></td>';
        echo '<td><a href="index.php?action=delete&id='.$sqlSeleciona->id_categoria_pagina.'" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-remove"></span> Excluir</a></td>';
        echo "</tr>";   
    }
}
  • Obs: I’m not having problems related to logic but refer to design patterns.

  • 1

    Look.. The standard is Voce that does why the project is yours.. But to stay a beautiful business and with this "pattern" that Oce seeks would be recommended that there is no HTML so in internal classes and yes in the pages in which the class is manipulated..

  • Research on MVC at first.

  • Model view and controller is smooth on architectures like JSF or Spring the problem is that I am not using any framework, this was set at the beginning of the project, php does not have a more closed architecture which in my humble view is worse because it ends up accepting gambiarras like this.

3 answers

1

The ideal is to save the HTML of PHP logic, so the code can be reused without any side effects such as Cannot Modify header information another point is your class must have a single responsibility and your methods must perform only one task, selecionaTudo does two things manipulating the database and formatting a text output (which is not the responsibility of the Categoriadao class).

If there are few records displayed and select has no Where it can simplify the method this way:

public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    return $conexao->conectar()->query($sql)->fetchAll(PDO::FETCH_ASSOC));
}

Suggestions

Instead of creating a connection variable for each method in your class, you can make it a class member and call Pdoutil in the DAO constructor.

class CategoriaDao {
   private $conexao;

   public function __constructor($db){
     $this->conexao = $db;
   }

   public function selecionaTudo($sql) {
     return $this->conexao->query($sql)->fetchAll(PDO::FETCH_ASSOC);
   }
 }

The call would look like this:

$DAOCategoria = new CategoriaDao(PDOUtil::conectar());
$categoria = $DAOCategoria->selecionarTudo($sql);

Since you are not using any framework, one way to seperate php’s html is to use a template engine like the Smarty.

0

separate logic from view.

Deliver the object of the query, such as an array of items, in the (HTML) view make a simple while. So you are not obliged to use only Bootstrap and can enjoy in another project your class.

A hint is to have your Bootstrap class, so take the object with the items and apply its call in this class, it being specific to Boostrap.

0

Are you creating a pattern or want to follow a pattern? Before thinking about the code we have to think about the architecture of the project. Although you say that there is no problem of logic this method does not make "echo" in the opening of "<tr>" generating a potentially problematic table for browser engines that do not compensate for poorly created tags (ie up to 8, for example). It would also go against the principle of generality since your research will need to return in a "guaranteed" way a "id_categoria_pagina" and would also have problems with encapsulation, since the opening of the table and its closure would be in other classes. If they’re in the same entity (something like CategoriaView) might not be a problem, but still would seriously consider leaving what is "view" in the View. ;)

  • 1

    thanks for the criticism, although it does not answer the question.

Browser other questions tagged

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