ERROR "Call to a member function setCode () on a non-object in" [closed]

1

Programming is in object-oriented php and MVC.

I am making a user registry and saving it in the database, however the following error appears:

  

Call a member function setCode () 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();
}

    
asked by anonymous 13.09.2015 / 19:06

1 answer

2

I do not know how your entire code is, if you use require correctly, but from what I saw, you did not start the class using new , and you're already using the $usuario variable. You have to instantiate the class and assign a unique 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();
}
    
13.09.2015 / 22:57