Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66725 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16476 invoked from network); 20 Mar 2013 18:44:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2013 18:44:00 -0000 Authentication-Results: pb1.pair.com header.from=carlos.vini@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=carlos.vini@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.182 as permitted sender) X-PHP-List-Original-Sender: carlos.vini@gmail.com X-Host-Fingerprint: 209.85.212.182 mail-wi0-f182.google.com Received: from [209.85.212.182] ([209.85.212.182:35468] helo=mail-wi0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E4/9B-46127-E630A415 for ; Wed, 20 Mar 2013 13:43:59 -0500 Received: by mail-wi0-f182.google.com with SMTP id hi18so2210791wib.9 for ; Wed, 20 Mar 2013 11:43:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=IwqcLxNF9vkkdimhF/I+55hSQI/M/Y5eIqTKj9zaPWQ=; b=LMjkFn7K3tYYP2nEOTwujCtYz7WUxgNRdBsL4Vi8ThvdSLgD+EZXUYm5OzOkJHvEE4 ZxGaG6jsFQ5m33cbqS1YxsAMbQYL3j3CLx94n8rEAP4dktdTaakDkgJJhauTfFzSDH3g My0+nljXaXj6+loD02O6EAQVXS2kYyZnAdiycnCBlvj8YkyVS1z7A4zBfzhDcOT/CNpA XabLBSarrVjA0lzw8dUqR8jIkepys0dNlufCNtg0FwbMimn4CLgtYSe5JpvTLG7UhiRJ YrmbFNO8EECU682noXK04dSJSoG3sTuzFUspqJR7E9YmuvjtXksa/B3PKU3YRABcM45n X/lA== X-Received: by 10.194.10.202 with SMTP id k10mr12428781wjb.53.1363805036499; Wed, 20 Mar 2013 11:43:56 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.38.70 with HTTP; Wed, 20 Mar 2013 11:43:36 -0700 (PDT) In-Reply-To: References: Date: Wed, 20 Mar 2013 15:43:36 -0300 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Method check - Can someone create a RFC for it? From: carlos.vini@gmail.com (Carlos Rodrigues) Like Mike emaild me, i can just change my code to something like $obj->getImage()->getUrl(), where getImage() will return a mock object with getUrl() returning an empty string. But my request here is not about this case only. Imagine you have a web page with 3 blocks of information. Let's say "news", "partners" and "blog". Now if for some reason you didn't code it right, you might get a fatal error in one of these "blocks". I'd love to have a way to try/catch these blocks, Currently i can only do this using ajax, or running shell exec "php block.php" for each one. If we could catch fatal errors, or at least the "Call to a member function on a non-object", we could have this code in our Zend Framework implementations, wrapping each view in a try/catch, and showing a "error, sorry", in case the view has errors. - Carlos On Wed, Mar 20, 2013 at 3:33 PM, Carlos Rodrigues wrote: > Thx for your reply Patrick, > > I agree that it's partially syntactic sugar, as you said. But when you do this: > > $obj = new stdClass(); > echo $obj->foo; > > PHP will not if foo is not declared. It will output an empty string. > But if you have a method > > echo @$obj->foo(); > echo 'end'; > > It will give you a fatal error. > > Even using @ to suppress error messages it won't echo the 'end' part. > > Since short array syntax (like [1,2]) was not extremely necessary and > was accepted, maybe someones has a better idea than i had to address > this. > > Carlos > > On Wed, Mar 20, 2013 at 3:22 PM, Patrick ALLAERT wrote: >> 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