How can I continue executing the remaining code when an error is reported during PHP program execution?

Source: Internet
Author: User
Tags try catch
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.