On Thu, Jul 13, 2017 at 2:55 AM, Tony Marston <tonymars...@hotmail.com> wrote:
> "David Rodrigues" wrote in message news:CAEsg9X2ECG62i7Z_Nu11kqr7 > yvbkmucd3mxgt78ulampfx-...@mail.gmail.com... > >> >> The idea is good, but I don't know if it is more clear than an 'if()' >> > > I think that this proposal is totally ridiculous and should be shot down > in flames. My reasoning is as follows: > > 1) There should be a rule that anything which can be achieved with 5 lines > or less in userland code should be automatically excluded from becoming a > change to the language. The cost of changing the language in order to get > around the laziness of a small number of users versus the "benefit" of > making the language more complicated for the rest of us, as well as adding > to the maintenance burden is simply not worth it. > > 2) Proposals such as this violate the first rule of good programming which > is to provide code which is readable and therefore understandable by > others, and therefore maintainable. I don't know about you, but I find it > much easier to read words than symbols. The idea that the code "if > (is_array($foo))" is too verbose and should be replaced with a shorthand > symbol is just too ridiculous for word. If any more of these shortcuts is > implemented then I'm afraid that code will look too much like a bunch of > hieroglyphics and become far less readable. > > Just my 2 cents worth. > > -- > Tony Marston > > > Em 11 de jul de 2017 12:15 PM, "Mark Shust" <m...@shust.com> escreveu: >> >> Hello, >>> >>> I wanted to garnish feedback on a RFC proposal. This is just a concept at >>> this point, and is inspired by the null coalesce operator. >>> >>> Code will error if a non-array value is passed through a looping feature. >>> For example, this code: >>> >>> <?php >>> >>> $foo = "abc"; >>> >>> foreach ($foo as $bar) { >>> >>> echo $bar; >>> >>> } >>> >>> >>> will result in the error: >>> >>> PHP Warning: Invalid argument supplied for foreach() in test.php on >>> line 3 >>> >>> >>> To prevent this, the logical solution is to wrap this in an is_array >>> check: >>> >>> <?php >>> >>> $foo = "abc"; >>> >>> if (is_array($foo)) { >>> >>> foreach ($foo as $bar) { >>> >>> echo $bar; >>> >>> } >>> >>> } >>> >>> >>> This code runs successfully and does not error out. For a syntactic >>> sugar/improvement, this can be shorthand for executing the loop instead >>> of >>> wrapping the block within an is_array check: >>> >>> >>> <?php >>> >>> $foo = "abc"; >>> >>> foreach (??$foo as $bar) { >>> >>> echo $bar; >>> >>> } >>> >>> >>> Let me know your thoughts. >>> >>> >>> Cheers, >>> Mark >>> >>> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > The more I think about this, the more I think that the throwing a warning when the variable is not an array is the proper thing to do. The cost in missed mistakes is much larger than the tiny savings in code that some people would get. Walter -- The greatest dangers to liberty lurk in insidious encroachment by men of zeal, well-meaning but without understanding. -- Justice Louis D. Brandeis