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 <carlos.v...@gmail.com> 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 <patrickalla...@php.net> > wrote: >> 2013/3/20 Carlos Rodrigues <carlos.v...@gmail.com>: >>> 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php