Clarification of MVC archiving in PHP

Asked

Viewed 284 times

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).

  • 5

    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.

1 answer

2


Good afternoon friend,

In the MVC world, we have fundamentally 3 items to observe:


Model

Whenever you think about data manipulation, think about model. It is responsible for reading and writing data, and also for its validations.

View

Simple: the user interaction layer. It just displays the data, being it through an html or xml.

Controller

The person responsible for receiving all user requests. Its methods called actions are responsible for a page, controlling which model to use and which view will be shown to the user.


Knowing this, and looking at the code you entered the first snippet of code would be a view.

arquivo_view.php:

<form method="post" action="usuario_controller.php"> 
    <label> 
        Nome <input type="text" name="nome" /> 
    </label> 
    <label> 
        Email <input type="text" name="email" /> 
    </label> 
    <input type="submit" value="Enviar" /> 
</form> 

But then we would have to receive the POST of this form in our controller, who will decide which model use, and then which view display at the end.

File usuario_model.php

<?php
    class Usuario {
        private $nome;
        private $email;
        private $data_nascimento;

        public function getNome(){
            return $this->nome;
        }

        public function setNome($nome){
            $this->nome = $nome;
        }

        public function getEmail(){
            return $this->email;
        }

        public function setEmail($email){
            $this->nome = $email;
        }

        public function getDataNascimento(){
            return $this->data_nascimento;
        }

        public function setDataNascimento($data_nascimento){
            $this->data_nascimento= $data_nascimento;
        }

        public function exibir(){
            echo $this->nome . ' <br /> ' . $this->email . ' <br /> ' . $this->data_nascimento;
        }
    }
?>

File usuario_controller.php

<?php
    include('usuario_model.php');

    $usuario = new Usuario();
    $usuario->setNome($_POST['nome']);
    $usuario->setEmail($_POST['email']);
    $usuario->setDataNascimento($_POST['data_nascimento']);
    $usuario->exibir();
?>

Roughly speaking, and in a very basic way what you did within a very basic MVC model, that would be the idea. Always try to dismember the files according to their functionality to maintain a code sustainable and clean.

About the concepts of POO, I believe you have sinned in using functions with name get to change the value of a property of an object. As I did there you should have used the nomenclature set, and get to return the existing value.

Recommended articles:

MVC and PHP - Understanding the MVC standard in practice

Object-Oriented PHP for Beginners

Browser other questions tagged

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