|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
When I run the following code the following errors end up in my error log
file: PHP Notice: Undefined variable: n in C:\home\php\sandbox\error.php on line 14 PHP Fatal error:Call to a member function fun() on a non-object in C:\home\php\sandbox\error.php on line 14 <?php class Numpty { public function fun() { return 'Have fun!'; } } $n = new Numpty(); echo $n->fun(); unset($n); try { echo $n->fun(); //this is line 14 } catch (Exception $e) { echo 'Gotcha!'; } echo 'Done'; ?> You will notice that I have a 'try/catch' block around the line that is causing the error. Why doesn't this code catch the error? What is the PHP way of handling this situation? I'm reading up now on custom error handlers but I decided to post this now in case that leads to another wgc ;-) Daniel Klein |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Wed, 26 Mar 2008 16:04:59 +0100, Daniel Klein
<danielk@featherbrain.net> wrote: > When I run the following code the following errors end up in my error log > file: > > PHP Notice: Undefined variable: n in C:\home\php\sandbox\error.php on > line > 14 > PHP Fatal error:Call to a member function fun() on a non-object in > C:\home\php\sandbox\error.php on line 14 > > > <?php > class Numpty { > public function fun() { > return 'Have fun!'; > } > } > > $n = new Numpty(); > echo $n->fun(); > > unset($n); > > try { > echo $n->fun(); //this is line 14 > } > catch (Exception $e) { > echo 'Gotcha!'; > } > echo 'Done'; > ?> > > > You will notice that I have a 'try/catch' block around the line that is > causing the error. Why doesn't this code catch the error? > > What is the PHP way of handling this situation? > > I'm reading up now on custom error handlers but I decided to post this > now > in case that leads to another wgc ;-) http://bugs.php.net/bug.php?id=40014 When it's not a recoverable error error it will not be catchable in any way. Adjust your code to cope with this by checking more / perhaps using type hinting as in http://bugs.php.net/bug.php?id=40014. -- Rik Wasmus |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Wed, 26 Mar 2008 16:51:36 +0100, "Rik Wasmus"
<luiheidsgoeroe@hotmail.com> wrote: >On Wed, 26 Mar 2008 16:04:59 +0100, Daniel Klein ><danielk@featherbrain.net> wrote: > >> When I run the following code the following errors end up in my error log >> file: >> >> PHP Notice: Undefined variable: n in C:\home\php\sandbox\error.php on >> line >> 14 >> PHP Fatal error:Call to a member function fun() on a non-object in >> C:\home\php\sandbox\error.php on line 14 >> >> >> <?php >> class Numpty { >> public function fun() { >> return 'Have fun!'; >> } >> } >> >> $n = new Numpty(); >> echo $n->fun(); >> >> unset($n); >> >> try { >> echo $n->fun(); //this is line 14 >> } >> catch (Exception $e) { >> echo 'Gotcha!'; >> } >> echo 'Done'; >> ?> >> >> >> You will notice that I have a 'try/catch' block around the line that is >> causing the error. Why doesn't this code catch the error? >> >> What is the PHP way of handling this situation? >> >> I'm reading up now on custom error handlers but I decided to post this >> now >> in case that leads to another wgc ;-) > >http://bugs.php.net/bug.php?id=40014 >When it's not a recoverable error error it will not be catchable in any >way. Adjust your code to cope with this by checking more / perhaps using >type hinting as in http://bugs.php.net/bug.php?id=40014. Thanks Rik; interesting reading, that. I will look into type hinting but for now it appears as that isset() and the is_xxxx() collection of functions will handle much of this error checking, albeit it will require a bit more code than exception handling :-( Btw, did you mean to post a different link to 'type hinting'? Perhaps you meant http://www.php.net/manual/en/languag...ypehinting.php Daniel Klein |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Wed, 26 Mar 2008 17:40:56 +0100, Daniel Klein
<danielk@featherbrain.net> wrote: > On Wed, 26 Mar 2008 16:51:36 +0100, "Rik Wasmus" > <luiheidsgoeroe@hotmail.com> wrote: > >> On Wed, 26 Mar 2008 16:04:59 +0100, Daniel Klein >> <danielk@featherbrain.net> wrote: >> >>> When I run the following code the following errors end up in my error >>> log >>> file: >>> >>> PHP Notice: Undefined variable: n in C:\home\php\sandbox\error.php on >>> line >>> 14 >>> PHP Fatal error:Call to a member function fun() on a non-object in >>> C:\home\php\sandbox\error.php on line 14 >>> >>> >>> <?php >>> class Numpty { >>> public function fun() { >>> return 'Have fun!'; >>> } >>> } >>> >>> $n = new Numpty(); >>> echo $n->fun(); >>> >>> unset($n); >>> >>> try { >>> echo $n->fun(); //this is line 14 >>> } >>> catch (Exception $e) { >>> echo 'Gotcha!'; >>> } >>> echo 'Done'; >>> ?> >>> >>> >>> You will notice that I have a 'try/catch' block around the line thatis >>> causing the error. Why doesn't this code catch the error? >>> >>> What is the PHP way of handling this situation? >>> >>> I'm reading up now on custom error handlers but I decided to post this >>> now >>> in case that leads to another wgc ;-) >> >> http://bugs.php.net/bug.php?id=40014 >> When it's not a recoverable error error it will not be catchable in any >> way. Adjust your code to cope with this by checking more / perhaps using >> type hinting as in http://bugs.php.net/bug.php?id=40014. > > Thanks Rik; interesting reading, that. > > I will look into type hinting but for now it appears as that isset() and > the > is_xxxx() collection of functions will handle much of this error > checking, > albeit it will require a bit more code than exception handling :-( Yup, also valuable the check for interfaces, allthough the instanceof operator is prefered nowadays. > Btw, did you mean to post a different link to 'type hinting'? Perhaps you > meant > http://www.php.net/manual/en/languag...ypehinting.php Indeed, there seems to be wrong with either my copying or pasting of links in my browser lately. That's the URL I ment. -- Rik Wasmus |
|
![]() |
| Outils de la discussion | |
|
|