ERROR " Call to a Member Function setCodigo() on a non-object in"

Asked

Viewed 2,393 times

1

The programming is in object-oriented php and MVC.

I am registering users and saving it in the database, but the following error appears:

Call to a Member Function setCodigo() on a non-object in

what is causing this error?

if ($_POST) {
    $codigo = isset($_POST["codigo"]) ? $_POST["codigo"] : false;
    $nome = isset($_POST["nome"]) ? $_POST["nome"] : false;
    $usuario = isset($_POST["usuario"]) ? $_POST["usuario"] : false;
    $senha = isset($_POST["senha"]) ? $_POST["senha"] : false;

    $usuario->setCodigo($codigo);
    $usuario->setNome($nome);
    $usuario->setUsuario($usuario);
    $usuario->setSenha($senha);

    $usuarioDao->salvar($usuario);
    header("location: ListaUsuario.php");
    exit();
}

inserir a descrição da imagem aqui

  • Welcome to Sopt, it would be nice for you to edit your question by adding your codes so the staff can help you :D

  • Welcome to Sopt, please post the class that contains the setCodigo function, so we can help you, you are giving a require correctly, it is with public encapsulation, private... ?

1 answer

3


I don’t know what your entire code looks like if you use require correctly, but from what I saw, you didn’t start the class using new, and is already using the variable $usuario . You have to instantiate the class and assign a single variable to it!

Example:

if ($_POST) {
    $codigo = isset($_POST["codigo"]) ? $_POST["codigo"] : false;
    $nome = isset($_POST["nome"]) ? $_POST["nome"] : false;
    $usuarios = isset($_POST["usuario"]) ? $_POST["usuario"] : false;
    $senha = isset($_POST["senha"]) ? $_POST["senha"] : false;

    $usuario = new Usuario();
    $usuario->setCodigo($codigo);
    $usuario->setNome($nome);
    $usuario->setUsuario($usuarios);
    $usuario->setSenha($senha);

    $usuarioDao->salvar($usuario);
    header("location: ListaUsuario.php");
    exit();
}
  • Hello, the user instance, is in the line above my code, but it is difficult to post all the code to find the error.

  • The programming consists of three classes: Formuser.php(Contains the form fields and variables they receive from $_POST), Usergo.php(Contains methods that have the function of saving, retrieving, listing and deleting a user) and User.php(the photo class above, containing the attributes getters and setters).

  • Hello, but I saw that you did: $usuario = isset($_POST["usuario"]) ? $_POST["usuario"] : false; and then guided using this variable $usuario, even if she was initiated, when she got to that part of the code, she was worth something else.

  • 2

    Thank you Cassiano José! was the same mistake! I was using the $user variable to receive it from $_POST and for the class.

  • 1

    For nothing! It always happened to me.

Browser other questions tagged

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