Clarification of MVC architectures in PHP [duplicate]

0

Good afternoon guys, I'm starting with php OO and with MVC, I'd like to know if the code snippet runs short of the standards, and if there's any improvement to be implemented. Thanks in advance for your help.

   <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 some time I've already programmed with Java, the class name question and the broken encapsulation pillar I realized, this example I found on the net giving a quick researched. I found it strange in a file to create the class and instantiate it in the same file. If it was in java it 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
    }
    
asked by anonymous 05.04.2017 / 20:36

1 answer

2

Good afternoon, my friend,

In the MVC world, we basically have 3 items to look at:

Model

Whenever you think of data manipulation, think of model. He is responsible for reading and writing data, as well as for his validations.

View

Simple: the user interaction layer. It just displays the data, either by means of an html or xml.

Controller

The 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 reported the first snippet of code would be a view .

file_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 get the POST of this form in our controller , which will decide which model to use, and then which view to display in the end.

File user_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;
        }
    }
?>

User_controller.php file

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

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

Roughly, and in a very basic way what you did inside a very basic MVC model, the idea would be this. Always try to break down the files according to their functionality to maintain a sustainable and clean

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

Recommended articles:

MVC and PHP - Understanding the MVC standard in practice

PHP Object Oriented for Beginners

    
05.04.2017 / 21:04