Hej,
I use PHP 5.2.6. I am refactoring some code to use more
object-orientation. I encounter a problem, where the new object-oriented
version results in a fatal error, where the old array-oriented version
didn't.
I fetch records a database. Sometime it happens that a record does not
exist anymore. Let's assume it's a user, then $user will be NULL.
echo $user['fullname']; // no error at all, $user['fullname'] === NULL
Shouldn't this at least trigger a Notice?
echo $user->get_fullname(); // Fatal error
I agree this should trigger an error, but a "Fatal error" is a little
too much, I think. It terminates the script leaving the html-document
incomplete. I would prefer a "Warning" and NULL
instead.
Is there any reason against it?
Best regards
Christopher
Hi
2008/11/17 Christopher Vogt christopher.vogt@rwth-aachen.de:
Hej,
I use PHP 5.2.6. I am refactoring some code to use more
object-orientation. I encounter a problem, where the new object-oriented
version results in a fatal error, where the old array-oriented version
didn't.I fetch records a database. Sometime it happens that a record does not
exist anymore. Let's assume it's a user, then $user will be NULL.echo $user['fullname']; // no error at all, $user['fullname'] ===
NULL
Shouldn't this at least trigger a Notice?
echo $user->get_fullname(); // Fatal error
I agree this should trigger an error, but a "Fatal error" is a little
too much, I think. It terminates the script leaving the html-document
incomplete. I would prefer a "Warning" andNULL
instead.Is there any reason against it?
I personally don't really mind it, as you got the instanceof and
typehinting to check for whenever a variable is an object, so I would
say its more of a user design issue. Fatal errors just requires you to
refactor your code so your code shouldn't emit such things which I'm
alright with.
Best regards
Christopher
--
--
Kalle Sommer Nielsen
Hi Kalle,
I personally don't really mind it, as you got the instanceof and
typehinting to check for whenever a variable is an object, so I would
say its more of a user design issue. Fatal errors just requires you to
refactor your code so your code shouldn't emit such things which I'm
alright with.
fatal error can also happen caused by a bug. The case described in my
Mail is even rather likely. However a fatal error prevents me from
handling the error using an error_handler. THAT is the problem.
Best regards
Christopher
Hi.
Christopher Vogt wrote:
I fetch records a database. Sometime it happens that a record does not
exist anymore. Let's assume it's a user, then $user will be NULL.echo $user['fullname']; // no error at all, $user['fullname'] ===
NULL
Shouldn't this at least trigger a Notice?
Check your error handling settings, probably warnings/notices are disabled.
echo $user->get_fullname(); // Fatal error
Well, is $user an object? If not, well, better not call a method on it.
Regards,
Karsten
PS: I am not sure this is a topic for php.internals...
Hi Karsten,
echo $user['fullname']; // no error at all, $user['fullname'] ===
NULL
Shouldn't this at least trigger a Notice?
Check your error handling settings, probably warnings/notices are disabled.
reproduce code:
<?php
error_reporting(E_ALL|E_STRICT);
$user = NULL;
echo $user['fullname'];
?>
Best regards
Christopher
Hej,
echo $user['fullname']; // no error at all, $user['fullname'] ===
NULL
Shouldn't this at least trigger a Notice?
Check your error handling settings, probably warnings/notices are disabled.
I now found it in the documentation os the String type
http://php.net/language.types.string
"Note: Accessing variables of other types using [] or {} silently
returns NULL."
I created a change request: http://bugs.php.net/bug.php?id=46602
Best regards
Christopher