5 ms·
If you find an odd situation in PHP due to backwards compatibility or whatever, I highly recommend you just make a wrapper function to do what you want. Anothe
by birken 14y ago
If you find an odd situation in PHP due to backwards compatibility or whatever, I highly recommend you just make a wrapper function to do what you want.
Another example of a common mistake with default functionality:
Did you know
json_decode('null')
and
json_decode('{')
both return the same thing, NULL. One is correct, one isn't. The only way to actually tell the difference? Also call json_last_error() to make sure there wasn't an error parsing the json. Pretty inconvenient to do every time (and most people wont). Solution: Wrap it.
At Thumbtack we have lots of wrapper functions which take care of situations where a built-in function doesn't have an interface we like. Most of them are purely for convenience though, not situations like this where the default output does something potentially misleading.
- masklinn 14y ago> Solution: Wrap it. Conversely, cut your losses and use something saner with better internal and API coherence.
- maratd 14y agoPlease take your hipster elitism somewhere else. Every language has warts and learning how to avoid them is always a good idea.
- msbarnett 14y agoFalse equivalence. The fact that every language has warts does not mean that all languages are equally warty. There rapidly comes a point when you're spending so much time working around PHP's prolific warts that it would be more efficient for you to move to a less warty language.
- kstrauser 14y agoThat's not elitism; that's reality. Notice that PHP is pretty much the sole language garnering this reaction? No one actively makes fun of C#, Python, Clojure, Haskell, Perl, Java, etc., even when they dislike them. For example, I don't care for Java at all, but I admit that it's a really good implementation of a language I just don't happen to like very much. Same for C#. My reasons for not using it have nothing to do with the language itself (which is perfectly decent by all accounts). Programmers joke about Perl looking like line noise, but even that's a criticism of how some people use the language and not anything bad about Perl directly. But no one other than PHP programmers seems to have anything good to say about PHP. It's nearly universally disrespected as an insecure, inconsistent, buggy mess. That's very unusual - perhaps unique - among major programming languages. Maybe there's a reason that non-Java programmers have a grudging respect for Java, non-Python programmers can find things they like about Python, but non-PHP programmers almost never have anything good to say about PHP. When a lot of subject matter experts vocally dislike something, maybe that thing is misunderstood. When nearly all of them dislike it, perhaps there's a reason for it.
- icelancer 14y ago"No one actively makes fun of C#, Python, Clojure, Haskell, Perl, Java, etc., even when they dislike them." This is laughable. Especially Java. http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- wvenable 14y ago> But no one other than PHP programmers seems to have anything good to say about PHP. The people least familiar with the language have the most negative to say about it. Making fun of PHP is pretty much a fad -- this particular topic, about htmlspecialchars(), I heard originally 5 years ago and it just cycles around and around. Even the most famously quoted articles about PHP's shortcomings are typically at least 50% flat-out wrong and 25% opinion. That being said, PHP has a fair share of warts -- it's a language with a long history and rapid change with little overall direction.
- kstrauser 14y agoMy experience is to the contrary: the people who've had to use it at work (as opposed to voluntarily for their own projects) are the ones who despise it most. It looks pretty neat at first, after all. It's only when you've dug deeply into it - and have experience in other languages to compare it to - that you genuinely start to loathe it.
- mikeash 14y agoI criticized PHP for years without ever really using it. Then I finally did some real work in the language. Did my opinion change from the exposure? You bet it did. Turns out that PHP is even worse than I had thought. I know plenty of people experienced with PHP who actively criticize it. People who have never used it generally tend not to care about it.
- brendoncrawford 14y ago> The people least familiar with the language have the most negative to say about it My 12 years of experience with PHP would beg to differ. It is, by ANY standard, a terrible language. In fact, it's primary defense of being "convenient" is not even true. Most developers would probably get a Django site up and running in the same amount of time it would take them to get a Cake or CodeIgniter site up and running.
- icelancer 14y agoOh thank god, I've never heard someone on HN say that PHP sucks and that you should switch. Thanks for bearing that cross for us.
- birken 14y agoAt Thumbtack we have 2 main repositories, our PHP repo and our Python repo. Our python repo was built from the group up and we use it for lots of things, in fact I think it is larger than our PHP repo. However, our core website is written in PHP, and will always be written in PHP for the reason that is always has been. Had I been a co-founder and been a part of making that decision I might have made a different one, but I wasn't and it doesn't matter. Pragmatic decision making is the name of the game. I admit that PHP doesn't have the best internal interfaces, however convenience functions with a whatever interface you want go a long way. I like sets in python, well we have tt_set() in PHP with exactly the same interface. I like defaultdicts in python, we have tt_default_dict() with the same interface. I like list.get() in python, we have tt_array_get() in PHP. I like namedtuples() in python, we have a NamedTuple() class in PHP which provides similar functionality (this one is not perfect though, a little more verbose to instantiate in PHP). I like string.startswith(), string.endswith(), and string slicing in python, we have tt_str_startswith(), tt_str_endswith(), tt_str_slice(). I like csv.DictReader() in python, we have CsvUtils/DictReader() in PHP. At the end of the day though, PHP is much more similar to Python than it is different, regardless of any of these convenience functions and wrappers. Potentially investing a significant amount of effort to switch something from one to the other strikes me as a very poor investment.
- pavel_lishin 14y ago> PHP is much more similar to Python than it is different Can you elaborate on this? Compared to what?
- pavel_lishin 14y agoObligatory "not always an option" response to your obligatory "PHP sucks, use something else" response.
- xxpor 14y agoSometimes that's not always a choice. OpenGL uses the same check for error pattern.
- masklinn 14y agoOpenGL is a C API. This is an acceptable pattern for a language at the level of C, without such features as exceptions, multiple return values or sum types. Which is not the case of PHP, which has both multiple return values (kind-of) and exceptions, and had them long before the json* functions where added.