# Fatal error: Call to a member function on a non-object

- Thread: https://externals.io/message/41956
- Original archive: https://news-web.php.net/php.internals/41956
- Started by Christopher Vogt on 2008-11-17T08:01:09+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #41956 — Christopher Vogt — 2008-11-17T08:01:09+00:00

URL: https://externals.io/message/41956#41956

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

---

## #41957 — Kalle Sommer Nielsen — 2008-11-17T08:16:13+00:00

URL: https://externals.io/message/41956#41957 · In reply to: #41956

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" and `NULL` 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

---

## #41959 — Karsten Dambekalns — 2008-11-17T09:31:29+00:00

URL: https://externals.io/message/41956#41959 · In reply to: #41956

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

---

## #41960 — Christopher Vogt — 2008-11-17T09:47:41+00:00

URL: https://externals.io/message/41956#41960

Hej Jochem,

I understand there are many PHP beginners flooding the wrong lists with
the wrong questions, so I don't mind your harsh response. But I am not
one of them.

> please don't post this kind of question to internals. use php-general@lists.php.net.

This was/is a question if something is worth a change request. This
concerns the development of PHP and in my eyes belongs on internals. Am
I mistaken here?

> plenty, I suggest taking the time to get a better understanding of OO,
> the php implementation and the various related tools it offers
> (instanceof, "method-chaining", exceptions, etc, etc).

I have a good understanding of OOP. This is not a start for me. I am
just refactoring existing PHP code to be object-oriented. You say there
are plenty of reasons for a Fatal error, so please tell me a few, so I
understand the reasons.

> calling a method on an object that doesn't exist is tantamount to calling a function
> which doesn't exist ... both are a fatal error.

Yes and maybe that is wrong two. But besides that, there is a difference
between the two. It hardly happens that a bug results in a call to a
non-existing function. But a bug can easily lead to an uninitialized
variable which is then treated as an object.

The problem with Fatal errors is that there is no way for me to handle
them. I use an error_handler in the production system. When an error or
unhandled exception occurs it displays an end-user-friendly error
message and then sends an email to our team's mailbox. Working with
arrays I can handle all sensible run-time errors using this methods.
Working with objects, I apparently cannot, because Fatal Error aren't
handled by the error_handler.

That's a serious problem because it completely hides a likely group of
errors from the notification system. I hopes this motivates the question
a little better.

But the question remains. Are there reasons to have a Fatal error here?

For comparison: Python throws an exception in a comparable case,
allowing me to handle the error.

Best regards

Christopher

---

## #41961 — Christopher Vogt — 2008-11-17T09:51:18+00:00

URL: https://externals.io/message/41956#41961 · In reply to: #41957

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

---

## #41963 — Christopher Vogt — 2008-11-17T10:06:27+00:00

URL: https://externals.io/message/41956#41963 · In reply to: #41959

Hi Karsten,

> > […]
> 
> 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

---

## #41964 — pstradomski@gmail.com — 2008-11-17T10:34:13+00:00

URL: https://externals.io/message/41956#41964 · In reply to: #41960

W liście Christopher Vogt z dnia poniedziałek 17 listopada 2008:

> I have a good understanding of OOP. This is not a start for me. I am
> just refactoring existing PHP code to be object-oriented. You say there
> are plenty of reasons for a Fatal error, so please tell me a few, so I
> understand the reasons.
You're calling a method. This requires code execution, but the code isn't 
there. If you were accessing a property of non-existing object (that is, if 
you were doing $user->fullname;) then you'd get a warning and NULL. It's not 
so for functions, as they're request for code execution, not for value 
(remember, not all functions return values as they most important effect - 
it's not Haskell).

Calling non-existing function returns in fatal error in PHP; it's the same 
for:
$object->iHaveNoSuchMethod();
$null->method();
nosuchfunction();

-- 
Paweł Stradomski

---

## #41984 — Christopher Vogt — 2008-11-18T09:49:19+00:00

URL: https://externals.io/message/41956#41984 · In reply to: #41959

Hej,


> > […]
> 
> 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
