Re: [PHP-DEV] Fatal error: Call to a member function on a non-object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Christopher Vogt schreef:
> 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.

I disagree. this kind of thing belongs on php-general (and lets keep it on list
please), if you have a serious proposal/rfc and/or one develops from a discussion
there then likely some of the old-hats will likely recommend escalating to
internals.

>> please don't post this kind of question to internals. use php-general@xxxxxxxxxxxxxx
> 
> 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.

fine, so your not an OO noob. nonetheless your going have to get accustomed
and intimate with php's implementation which is what it is, it works, it's consistent
but may not conform to all your expectations that you bring with you from other
languages ... it's a question of you adapting to php not the other way around.

if everyone who came to a language had that language implementation changed
to meet their expectations of what it should be doing and how said language
would quickly deteriorate into complete shite ... something noone would want to
use or develop.

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

yes, it tells you your doing something impossible.

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

PHP != Python. python is pure OO. php is hybrid. you have to refactor your
code in different ways.

if your writing code that blindly makes use of a $user var that may or may not
be a valid object then you code is incorrect. not being able to trap Fatals is
essentially a non-issue as your code should be structured so that they cannot occur.

there are a number of ways of dealing with the issue you have.

1. test the validity of the object

if ($user instanceof User) {
	// do something
}

2. start using exceptions

try {
	$user = UserGetter::get($id);

	// do stuff with $user
} catch (UserException $e) {
	// handle exception
}

// where UserGetter::get() is something like ...

class UserGetter {
	static function get($id) {		
		$data = getUserDataFromDB($id)
		if ($data)
			return new User($data);

		throw new UserException("$id is invalid");
	}
}

what this comes down to is this, python gives you exceptions free of
charge, php asks you to implement and throw them yourself ... this is
primarily the result of php's procedural legacy. ce la vie.

> 
> Best regards
> 
> Christopher


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux