0
Good afternoon guys, I’m starting with php OO and MVC, I wonder if the portion of code below escapes the standards, and if there is any improvement to be implemented. From now on I thank all those who collaborate.
<form method="post" action="getdata.php">
<label>
Nome <input type="text" name="nome" />
</label>
<label>
Email <input type="text" name="email" />
</label>
<input type="submit" value="Enviar" />
</form>
PHP class
<?php
class getData{
private $nome;
private $email;
public function getNome(){
$this->nome = $_POST['nome'];
}
public function getEmail(){
$this->email = $_POST['email'];
}
public function exibir(){
echo $this->nome . ' <br /> ' . $this->email;
}
}
$getData = new getData();
$getData->getNome();
$getData->getEmail();
$getData->exibir();
?>
I’m starting with PHP now but for some time I already program with Java, the question of the class name and the pillar of broken encapsulation I noticed, this example I found on the net giving a quick search. I found it strange in a file to create the class and instantiate it in the same file. If it were in java would do as follows
public class ProcessaDados extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
//recuperaria os dados
String nome = req.getParameter("nome");
String email = req.getParameter("email");
//criaria o Modelo e realizaria o restante da regra
}
get
usually is to extract some kind of information and not assign (set
).– rray
MVC is not technology is "way of doing things" and your code there’s nothing "MVC". Note that OO and MVC do not define quality and organization, they help, but if you don’t know what you’re doing, then it just gets worse. An MVC can be built without an OO, but that’s not the point, what I mean is that if you don’t have real and necessary reasons to use it, if it’s something simple only a few ifs and variables in PHP solve. If you want to use something "functional" and is a large project (with many people) and may present some complexity so look for an already popular framework, do not re-invent the wheel.
– Guilherme Nascimento