Error in __toString PHP7 function

Asked

Viewed 670 times

-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 mistake:

Fatal error: Method Usuario::__toString() must not throw an Exception, Caught Error: Call to a Member Function format() on null in

  • 1

    The error is not in the method __toString. What instance getDtcadastro returns? If possible put the code too.

  • 1

    Apparently you have one new throw execption() or some instruction has launched an Exception there

  • It seems that your registration date is null and the format method cannot format your date from null.

1 answer

1

The Error

Fatal error: Method Usuario::__toString() must not throw an Exception, Caught Error: Call to a Member Function format() on null in

It’s happening precisely because $this->getDtcadastro() is null (null) and also before giving the return and the json_encode give a print_r in his array and make sure it’s not empty.

Browser other questions tagged

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