Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66723 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12902 invoked from network); 20 Mar 2013 18:22:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2013 18:22:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=patrick.allaert@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=patrick.allaert@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.47 as permitted sender) X-PHP-List-Original-Sender: patrick.allaert@gmail.com X-Host-Fingerprint: 209.85.219.47 mail-oa0-f47.google.com Received: from [209.85.219.47] ([209.85.219.47:43732] helo=mail-oa0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E9/DA-46127-75EF9415 for ; Wed, 20 Mar 2013 13:22:15 -0500 Received: by mail-oa0-f47.google.com with SMTP id o17so2128636oag.6 for ; Wed, 20 Mar 2013 11:22:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=nXk0AyybnCLqk906MGxfXIpGQPh2d+FY6AfgH/1EDRQ=; b=T1VS4nHJZX4V4TcJ5QY4+iVSbD8BNZCe/i0h+/9zK2Ba3Jb1k1fSiRspRfaTzYRa59 TQN+pwhSYqfSuzoc3UYNz6XelgEYUNPMr2Cz8l1wr9mu+IIu8LjKGtI6yDKEvH0ML/Gt jdLIVRHcQHmflTY7LFNkCwDyLq2DNmEbfEagno/dkI/B0rUlVzefUexj/GefqI96AdMt jfSDYwVuM5mczLrDXX9UTvbYZzWrX9b/RNQWRfXIom5aUcmpE1jSbWKxq2Bd2gXQR5JV sl/esTseK59XPRUaec7M3nBQ25aetOli19khbdJA6WiqWuaFV2UF75TaWm/LGqTJIM6Q 9Uwg== MIME-Version: 1.0 X-Received: by 10.60.172.84 with SMTP id ba20mr4746919oec.10.1363803732662; Wed, 20 Mar 2013 11:22:12 -0700 (PDT) Sender: patrick.allaert@gmail.com Received: by 10.76.163.38 with HTTP; Wed, 20 Mar 2013 11:22:12 -0700 (PDT) In-Reply-To: References: Date: Wed, 20 Mar 2013 19:22:12 +0100 X-Google-Sender-Auth: C5Av4PoRICTzlnKi5R0gkarewd4 Message-ID: To: Carlos Rodrigues Cc: PHP Development Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Method check - Can someone create a RFC for it? From: patrickallaert@php.net (Patrick ALLAERT) 2013/3/20 Carlos Rodrigues : > 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. That's syntactic sugar we can live without as it does not really encourage good programming practices. If you don't care about those, then you can already just do: echo @$obj->image->getUrl(); Patrick