Error in function __toString PHP7

-3

What is the solution to the error below?

public function __toString(){

        return json_encode(array(

            "idusuario"   =>$this->getIdusuario(),
            "deslogin"    =>$this->getDeslogin(),
            "dessenha"    =>$this->getDessenha(),
            "tdcadastro"  =>$this->getDtcadastro()->format("d/m/Y - H: i: s")

        ));

}

This is the error:

  

Fatal error: Method User :: __ toString () must not throw an exception, caught Error: Call to a member function format () on null in

    
asked by anonymous 23.06.2017 / 05:09

1 answer

1

The Error

  

Fatal error: Method User :: __ toString () must not throw an exception, caught Error: Call to a member function format () on null in

It is occurring precisely because $this->getDtcadastro() is null ( null ) and also before giving return and json_encode give print_r in your array and make sure it is not empty.

    
23.06.2017 / 06:47