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

From: Date: Mon, 17 Nov 2008 08:44:31 +0000
Subject: Re: Fatal error: Call to a member function on a non-object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-283341@lists.php.net to get a copy of this message
please don't post this kind of question to internals. use php-general@lists.php.net. Christopher Vogt schreef: > 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. either go back to using arrays or fix your code. your asking for something to change when you have little idea of why or what it is in the first place. > > Is there any reason against it? 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). 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. > > Best regards > > Christopher >

« previous php.general (#283341) next »