How do I get the rest of the code to execute when the PHP program executes an error?

Source: Internet
Author: User
Tags try catch
For example, the following code, the result of printing only shows the public, and then reported a fatal error, then the occupation skip the mistake to let the next program continue to execute it? The desired 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 capture processing under

Is that so? But the second line of error, the code behind will not be executed AH
try {
Echo $obj->protected;
} catch (Emptyiterator $e) {
Print $e->getmessage ();
}

Cannot access protected property MyCLass:: $protected
Unable to access protected resource

This is a fatal error, not to be avoided!
In other words: You don't know the rules.

You can use simple debugging functions to observe the properties of an object
Print_r ($obj);
Or
Var_dump ($obj);


A fatal error is no way to skip, and once it happens, it stops running.

Thanks, everybody.

  • 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.