Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66724 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14915 invoked from network); 20 Mar 2013 18:40:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2013 18:40:55 -0000 Authentication-Results: pb1.pair.com header.from=nathan.bruer@starin.biz; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=nathan.bruer@starin.biz; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain starin.biz from 207.198.105.69 cause and error) X-PHP-List-Original-Sender: nathan.bruer@starin.biz X-Host-Fingerprint: 207.198.105.69 hawk.starindns.com Received: from [207.198.105.69] ([207.198.105.69:46550] helo=hawk.starindns.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/3B-46127-5B20A415 for ; Wed, 20 Mar 2013 13:40:54 -0500 Received: from 173-167-160-145-illinois.hfc.comcastbusiness.net ([173.167.160.145]:59292 helo=NathanBruerHP) by hawk.starindns.com with esmtpa (Exim 4.80) (envelope-from ) id 1UINwO-00075N-IE; Wed, 20 Mar 2013 13:40:48 -0500 Sender: "Nathan Bruer" To: "'Carlos Rodrigues'" , References: In-Reply-To: Date: Wed, 20 Mar 2013 13:40:56 -0500 Message-ID: <2bd501ce259a$75e27610$61a76230$@starin.biz> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQHld8L/K6OsPkvtu0delkAUdjRM6piAP7xQ Content-Language: en-us X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hawk.starindns.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - starin.biz X-Get-Message-Sender-Via: hawk.starindns.com: authenticated_id: nathan.bruer@starin.biz Subject: RE: [PHP-DEV] Method check - Can someone create a RFC for it? From: nathan@starin.biz > Hi, > > I'd like to suggest a new functionality for PHP. I don't know the internals, and i can't create a patch implementing this. I'm writing here in case someone likes this idea and write a RFC. > > We've had a problem recently where one of our developers forgot an "if". > > So instead of writing > if ($obj->image) { > echo $obj->image->getUrl(); > } > > He wrote: > echo $obj->image->getUrl(); > > Here, locally, it was working cause we had the image. But when we updated the online version we got a "Fatal error: Call to a member function getUrl() on a non-object" cause someone didn't upload the image in one of the records that we're on the home page. > > Fatal errors like this can't be catched by a try/catch. And since this > getUrl() was not essential for the page, we'd better output an empty string then having the site offline. > > One might say: "you guys should have tested it better", or "The image field should be mandatory in the back end." > And it's true, he should have written that extra "if {}". > > Examining this problem we happened to stumble open Ruby's and Coffescript's method check. > Something like this: > > $obj->image?->getUrl()?; > > So my suggestion is: > > Create something like $foo->bar?() or $foo->bar()?, where you don't care whether the function exists, or if $foo is an object. If it doesn't exist you just return null. > I guess there are plenty of systems out there where it's better to output an empty string than having your site offline, just cause the programmer couldn't test it completely. > > Thanks for reading it. > > Carlos Rodrigues I do not like this idea. The error message "Fatal error: Call to a member function getUrl() on a non-object" is very straight forward, it should tell you the line the problem is on and everything and a quick glance should spot the problem quickly. Granted Yes there are cases were it'd be nice to have an easy work around, but then you run into the problem similar to what the @ has caused for many people, which libraries and general developers start using it in many places to keep errors form being thrown and you end up with code that is even harder to debug because no errors are shown. Another solution that you could have used is to set $obj->image to a singleton object that is a placeholder for methods like this which might be set... something like: http://3v4l.org/utBWP see: http://www.php.net/manual/en/language.oop5.overloading.php#object.call But even that is a terrible solution, by showing the error fixed a bug... bugs need to be fixed.