PHP - How to keep program still run when have error
-
In the example code: <?php for ($i = 1; $i <=20; $i++) { echo $i . '<br />'; if ($i == 10) { $haha->hoho("hehe"); } } ?> When $i = 10 program will show this error, Call to a member function hoho() on a non-object because $haha is not a object and the program stops running. I want the program to handle this error and continue running to $i=20. How can I do it?
-
Answer:
The Simple answer: Fix your code. Long answer: There is lots of ways to do that, the first thing off the top of my head is to use http://php.net/manual/en/function.set-error-handler.php regardless of your programming pattern. But if you are doing it in OOP you should make use of http://php.net/manual/en/language.oop5.magic.php(http://au2.php.net/manual/en/security.magicquotes.what.php) like http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members and http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members -obviously the $haha needs to be the object in your example. Hint: Using http://php.net/manual/en/language.exceptions.php in magic methods is really good idea. but you can't handle this directly with exception because Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions.
Phi Tống at Stack Overflow Visit the source
Other answers
I would try something like: <?php for ($i = 1; $i <=20; $i++) { echo $i . '<br />'; if ($i == 10) { if ($haha) $haha->hoho("hehe"); } } ?> This should check if $haha exists before it tries to do $haha->hoho("hehe");
Mike Lentini
You can use try and catch block. An exception is thrown, when you call a method on non-object. See http://php.net/manual/en/language.exceptions.php for exeptions in php. <?php for ($i = 1; $i <=20; $i++) { echo $i . '<br />'; try { if ($i == 10) { $haha->hoho("hehe"); } } catch(Exception $e) { //handle the error... } } ?>
Jaa-c
Related Q & A:
- How do I fix a Spooler subsystem app error?Best solution by Yahoo! Answers
- Why is my old Yahoo! 360 account still active when my old yahoo account is no longer active?Best solution by Yahoo! Answers
- Why is my laptop still charging when the battery is not plugged in?Best solution by Super User
- Why does a nose run when you have a cold?Best solution by thesurvivaldoctor.com
- Is it good to run when your sick with a stuffed nose?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.