How can I continue executing the remaining code when an error is reported during PHP program execution? For example, in the following code, only public is displayed and a fatal error is reported. how can we skip this error to continue executing the program? The output format is
Public
Error
Error
Public protected private
Class MyCLass
{
Public $ public = 'public ';
Protected $ protected = 'protected ';
Private $ private = 'private ';
Function printHello ()
{
Echo $ this-> public;
Echo $ this-> protected;
Echo $ this-> private;
}
};
$ Obj = new MyCLass ();
Echo $ obj-> public;
Echo $ obj-> protected;
Echo $ obj-> private;
$ Obj-> printHello ();
Reply to discussion (solution)
Try catch
Is that true? But the second line still reports an error and the code will not be executed.
Try {
Echo $ obj-> protected;
} Catch (EmptyIterator $ e ){
Print $ e-> getMessage ();
}
Cannot access protected property MyCLass: $ protected
Unable to access protected resources
This is a fatal error and cannot be avoided!
In other words, you do not understand the rules.
You can use simple debugging functions to observe object attributes.
Print_r ($ obj );
Or
Var_dump ($ obj );
A fatal error cannot be skipped. Once a fatal error occurs, it stops running.
I have been taught. thank you!