I can’t do Insert in object orientation

Asked

Viewed 68 times

-2

I’m studying POO in php and I’m having a hard time, I’m making a simple platform that uses the CRUD (just to train).

I created my own class and and the views and yet I’m not getting the Insert, if I do in pure as ancient method it works, already in object orientation no

I don’t know what’s going on, someone could help me?

Follows the code

Model/Userinterno

class UserInterno{

    private $nome;
    private $sobrenome;
    // private $tipo;
    private $email;
    private $login;
    private $senha;


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

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

    public function getSobrenome(){
        return $this->sobrenome;
    }

    public function setSobrenome($sobrenome){
        $this->sobrenome = $sobrenome;
    }

    // public function getTipo(){
    //     return $this->tipo;
    // }

    // public function setTipo($tipo){
    //     $this->tipo = $tipo;
    // }

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

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

    public function getLogin(){
        return $this->login;
    }

    public function setLogin($login){
        $this->login = $login;
    }

    public function getSenha(){
        return $this->senha;
    }

    public function setSenha($senha){
        $this->senha = $senha;
    }

}

Controller/Serviceuserinterno

class ServiceUserInterno{

    //atributos
    private $db;
    private $user;

    function __construct(Mysqli $mysqli, UserInterno $user){
     //ja recebendo a conexao do banco
     $this->db = $mysqli;
     $this->user = $user;
    }
    public function insert(){
        $stmt = $this->db->stmt_init();
        $stmt->prepare("INSERT INTO tb_user_master (nome, sobrenome, email, login, senha) VALUES(?,?,?,?,?)");

        $nome_user = $this->user->getName();
        $sobrenome_user = $this->user->getSobrenome();
        // $tipo_user = $this->user->getTipo();
        $email_user = $this->user->getEmail();
        $login_user = $this->user->getLogin();
        $senha_user = $this->user->getSenha();


        $stmt->bind_param("ssisss", $nome_user, $sobrenome_user, $email_user, $login_user, $senha_user);
        $stmt->execute();
    }
}

Controller/register

<?php 
include('libs/conexao.php');
require_once ("Model/UserInterno.php");
require_once ("Controller/ServiceUserInterno.php");

$nome = $_POST['nome'];
$sobrenome = $_POST['sobrenome'];
$email = $_POST['email'];
$login = $_POST['login'];
$senha = $_POST['senha'];

$user = new UserInterno();

$ServiceUser = new ServiceUserInterno($mysqli, $user);


$ServiceUser->setNome($nome);
$ServiceUser->setSobrenome($sobrenome);
$ServiceUser->setEmail($email);
$ServiceUser->setLogin($login);
$ServiceUser->setSenha($senha);

$ServiceUser->insert();
?>

Views/view_crea_internal user

   <div class="row linha_form">
            <form action="../Controller/cadastrarUser_interno.php" method="POST">
                <div class="col-lg-12">
                    <input type="text" name="nome" placeholder="Nome*" class="input_user">
                    <input type="text" name="sobrenome" placeholder="Sobrenome" class="input_user">
                    <!-- <input type="text" name="tipo_area" placeholder="Formação profissional*" class="input_user"> -->
                    <input type="email" name="email" placeholder="E-mail*" class="input_user">
                    <input type="text" name="login" placeholder="Login*" class="input_user">
                    <input type="password" name="senha" placeholder="Senha*" class="input_user">
                    <br>
                    <input type="submit" name="cadastrar" value="Cadastrar" class="btn_cadastrar_user">
                </div>
            </form>
        </div>

ERROR: Uncaught Error: Call to Undefined method Serviceuserinterno::setNome() in /opt/lampp/htdocs/curso_php_mysql/sistema_KeM/Controller/cadastrarUser_interno.php:17 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/curso_php_mysql/sistema_KeM/Controller/cadastrarUser_internal.php on line 17

NOTE: the file register it receives the data from the post that was sent from the form and makes the

  • What exactly is the problem? any error message appears?

  • Line 6 is $stmt = $this->db->stmt_init(); ?

  • In my editor appears in this line > private $user;

  • Just this doesn’t seem to generate the error, you have some line like private $prop = new obj() or calls some function at property initialization?

  • Sorry, I updated my folder in apache (because I have to put my controller there in apache php, if not at the time of require it does not find) and this error appears... and at the time of set, I do not know why this way will not

  • $ServiceUser has no setNome or setAlgo who has property deprivation $user no way to call externally. You already pass the user to $ServiceUse nor does it make sense to call these set() the work is done 'twice'.

  • I do not understand, what Voce meant... How I will recover the post data in the Serviceuser class....

  • I’ve put together an answer, anything at all let me know.

Show 3 more comments

1 answer

1

Error happens because there is no method setNome() or setAlgo() in class ServiceUserInterno. This class has two dependencies to the first object of Mysqli and the second object User see in the builder.

$ServiceUser = new ServiceUserInterno($mysqli, $user);

If $user has setNome(), setSobrenome() etc and is passed to class that does the Insert (ServiceUserInterno) what is the meaning of the code below?

$ServiceUser->setNome($nome);
$ServiceUser->setSobrenome($sobrenome);
$ServiceUser->setEmail($email);
$ServiceUser->setLogin($login);
$ServiceUser->setSenha($senha);

None. You can remove it.

Information sent by the user must be stored by the class UserInterno. Change the code to:

$user = new UserInterno();

$user->setNome($nome);
$user->setSobrenome($sobrenome);
$user->setEmail($email);
$user->setLogin($login);
$user->setSenha($senha);

$ServiceUser = new ServiceUserInterno($mysqli, $user);
  • Oops... Thanks, I changed my logic, only I’m still very raw in POO... but thanks for the help

Browser other questions tagged

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