On 22 February 2012 20:04, Larry Garfield <la...@garfieldtech.com> wrote: > On 2/22/12 12:37 PM, Peter Lind wrote: > >>> I would also support this. There's a myriad reasons why something may >> >> return NULL or FALSE when you expect it to return an object, some of them >> even legitimate. Any function/method whose documentation line is "returns >> the foo object, or NULL if someone screwed up and there isn't one" is >> perfectly reasonable in many cases, IMO, but makes all chains a potential >> fatal. An exception would make a lot more sense, and allow us to >> centralize handling of such "exceptional" cases rather than throwing >> if-checks everywhere. (Which is exactly what exceptions are for.) >>> >>> >>> --Larry Garfield >>> >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >> >> Seems to me this change would encourage bad habits (breaking the law of >> Demeter) which would personally put me against it. >> >> Regards >> Peter > > > How? What bad habit does this encourage? > > > --Larry Garfield >
As I wrote, the law of Demeter (http://en.wikipedia.org/wiki/Law_Of_Demeter) The OP started out with this code example, as a reason to introduce the change: $granny_name = $baby->getMother()->getMother()->getName(); The code shouldn't be aware of all the links, it's a bad habit. Rewrite to: $granny_name = $baby->getGrandmother()->getName() or $granny_name = $baby->getGrandmotherName(); And with the latter, you've reduced the amount of checks you have to do, by putting them in the proper places (if baby and mother objects inherit from the same class you've essentially got one check on null inside getMother(), not many spread out in client code). Also, the client code now only knows about the immediate surroundings - and should obviously know if getGrandmother() or getGrandmotherName() can throw exceptions. So as far as I'm concerned, the feature proposed just makes it a easier to couple your code more tightly. I'd rather the language discouranged that - or at least not encourage it. Regards Peter -- <hype> WWW: plphp.dk / plind.dk LinkedIn: plind BeWelcome/Couchsurfing: Fake51 Twitter: kafe15 </hype> -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php