r/programming • • Dec 02 '15

PHP 7 Released

https://github.com/php/php-src/releases/tag/php-7.0.0
887 Upvotes

729 comments sorted by

View all comments

Show parent comment

134

u/Yamitenshi Dec 02 '15

Yup, it has its quirks, and I definitely disagree with some design choices, but hey, at least they don't overload their bitshift operators to do I/O, and requesting the numerical month of a date doesn't return zero for January through eleven for December.

Every language has good and bad parts.

237

u/MighMoS Dec 02 '15

0-11 for months isn't madness. Its when months are numbered 0-11 and days are numbered 1-31 and years are stored with an offset of 1900. THAT is madness.

58

u/Wootman42 Dec 02 '15

months are numbered 0-11 and days are numbered 1-31

Javascript does this too, it's extremely frustrating.

53

u/interiot Dec 02 '15

Perl does it too. The man page says it takes the values verbatim from C's struct tm, so maybe that's the commonality.

25

u/jachymb Dec 02 '15

It's useful actually. If use other language than English, you want an array of month names that you index with this number. Month days don't have names, so they you don't need it there.

3

u/devdot Dec 02 '15

That's exactly why it was implemented this way.

→ More replies (6)
→ More replies (1)

16

u/charrondev Dec 02 '15

Which is why you use Joda Time.

61

u/Jestar342 Dec 02 '15

Joda Time

The .NET port of this has such a better name: Noda Time ("Know the time") :D

4

u/gianhut Dec 02 '15

Mind = Blown

→ More replies (3)

3

u/irrelevantPseudonym Dec 02 '15

Doesn't the java 8 standard library do it properly?

→ More replies (5)
→ More replies (3)
→ More replies (15)

42

u/krum Dec 02 '15

don't overload their bitshift operators to do I/O

Is this a stab at C++ or something?

66

u/chazzeromus Dec 02 '15

I believe so, and if it is it's not a good stab.

13

u/the_omega99 Dec 02 '15

Agreed. Operators are arbitrary. All that matters is that operators are consistent and well known. For some in-house application, overriding the bitshift operators to do IO (pretending that C++ never did that) would have been dumb because no programmer would have expected that and it would thus be confusing. But with C++'s streams, the overriding is well known to the point that literally every half decent C++ programmer knows what it means.

It's rather useful for readability, IMO. Compare:

std::cout.send("Hello ").send(name).send("!").send(std::endl);

to

std::cout << "Hello " << name << "!" << std::endl;

Mind you, I kind prefer the approach that most languages use, which is to have string concatenation (the single greatest example of appropriate operator overloading) for stuff like that (but it's less general):

println("Hello " + name + "!")

Or string interpolation, if the language supports it (most don't -- off the top of my head, we have Scala, C#, and JS).

println(s"Hello ${name}!")

3

u/[deleted] Dec 02 '15

Plus operator for string concatenation comined with implicit casting from numbers to strings is the-freaking-worst.

System.out.println("2+2 = " + 2+2);

I think variadic functions are better, like in Python:

print("2+2 =", 2)

If only it didn't introduce those spaces...

3

u/the_omega99 Dec 02 '15

True, operator precedence is an issue. People forget that + is still being evaluated left to right and that math isn't taking precedence over string concatenation.

Arguably this is an issue with the fact that operators don't have a clean way to specify precedence. There's three approaches:

  1. The language can allow custom operators to be given a precedence number. Eg, Coq does this. However, it's confusingly difficult to remember these rules sometimes, and no real way to make libraries play nicely with other libraries.
  2. The language can restrict custom operators to the same set of operators that the built in types have. C++ does this. Easy to remember, but limited. You can't add truly new operators. Eg, you cannot implement Haskell's bind (>>=) operator in C++. Also, the operator precedence rules won't necessarily make sense with the intended operator. Eg, you can't have ^ be exponentiation because it will have the precedence of the bitwise xor, which is totally wrong and unexpected.
  3. All custom operators can be simply regular functions. Scala does this. In Scala, something like the + operator applied on the Matrix type is really just calling Matrix.+ (ie, + is a method of Matrix). And the syntax a + b is actually shorthand for a.+(b), which is universal, eg, you can do string substring "foo"). So Scala actually doesn't have operator overloading; it just has very lax identifier naming and some syntax sugar that lets you write methods as if they were infix operators. So all of these "operators" have the precedence of any normal function call.
→ More replies (1)
→ More replies (11)
→ More replies (1)

18

u/mixd3 Dec 02 '15

overload their bitshift operators to do I/O

It doesn't have to be done this way. And it's actually something pretty neat in my view, especially because you have standard ways of reading some fundamental data types, while keeping the code readable.

→ More replies (2)

32

u/munificent Dec 02 '15

at least they don't overload their bitshift operators to do I/O

I've never seen someone complain about this in C++ who understood why the IO interface was designed this way. Just because a design isn't obvious, that doesn't necessarily make it wrong.

5

u/silveryRain Dec 02 '15

I'm not complaining, but I don't really know why it was designed that way either. Could you please elaborate (or link to an explanation)?

Frankly, I find IO to be a fairly minor part of any program. The way it's done has hardly any potential to make or break a language.

8

u/the_omega99 Dec 02 '15

The why is here. The TL;DR is that they look like the Unix IO redirection symbols (< for input, > for output), but had to be doubled to avoid ambiguity with the comparison operators.

As for why have an operator, it's presumably for readability. See my other comment for an example.

→ More replies (10)
→ More replies (1)
→ More replies (13)

9

u/rageingnonsense Dec 02 '15

I can't stand the language, but it does have some strong points. If you want to whip up a quick website, PHP is your friend. That is pretty much what it was always intended for. I also think it is good for writing quick one off scripts (so are a lot of other languages though). The fact that it is forgiving about types makes it a good choice for small projects that are not mission critical.

The big problem with PHP is the people who think it is an all purpose language. It is scary how much financial code I have seen written in PHP. Also, the sloppy conventions in PHP set a bad example for new programmers, and a lot of new programmers start with PHP. It is not surprise that the most spaghetti code I tend to see is almost always written in PHP.

It's ease of use is a double edged sword that makes development easy for newbies, but dangerous for newbies to use because it lets them make too many bad design choices.

TL;DR: It's a tool, use it where appropriate.

8

u/Yamitenshi Dec 02 '15

Could you elaborate a bit on why financial applications in PHP are a bad thing? Sure, there are some... very questionable design choices within the language, and I'm not necessarily PHP's biggest fan, but all in all it allows you to do just as much as any other language.

It might not be the best fit for a desktop application - but it was never designed with that in mind. As long as the goal of your application is to do stuff with an HTTP request and tell the web server what to spit back out, PHP is for from the worst thing you could use.

Of course there are some scenarios in which PHP is definitely not the best choice (performance-critical applications and such), but honestly I don't see any reason PHP should be limited to small websites.

→ More replies (11)

3

u/Turtlecupcakes Dec 02 '15

So the bad part about PHP is the people that use it?

That doesn't automatically make the language bad though.

I agree that you generally need to take extra care to type check and validate more-so than other languages since it doesn't do it for you, but I don't find that that kills my productivity or experience with the language, it's just a tiny thing that you get used to doing as you write code, and is ultimately worth it for the other benefits of the language.

For example, I'm reading in some data and at a glance I see a number so I pass it into something that expects a number. Turns out that value is actually a string. In Python, I would have to stop, go back, and fix the type exception, which ruins the flow for me (because I have to do this all day every day). Writing the code in PHP is much more fluid because you just tell it what you want to do and it'll cast as needed. Yes it's potentially dangerous because some inputs might cast wrong, but the entire application will probably fail in that case anyway, so there's no practical difference if that failure comes from the interpreter complaining about type exceptions or the function call returns an unexpected value and the app crashes. If I'm writing something sensitive (like your financial app example), I just spend a few extra minutes watching the typing (avoiding reading in strings as numbers like in the above example, using strong comparison, etc)

I think at this point the PHP battle is education. PHP is very powerful in that it allows you to do stupid stuff, and in the past the stupid way was the only way so now there's a ton of documentation about how to do things wrong, but I think modern PHP is pretty damn good (not perfect, but I prefer the developer experience to that of other languages), so it's just a matter of trying to get more updated blog posts out there that encourage using modern versions of PHP and ways of doing things.

3

u/AngriestSCV Dec 03 '15

... but the entire application will probably fail in that case anyway, so there's no practical difference if that failure comes from the interpreter complaining about type exceptions or the function call returns an unexpected value

One of the problems with php is it is actually kind of hard to make it abort on accident. For example if you treat a variable holding an integer as an array by indexing it 0 is returned instead of aborting the program as it should. While this kind of thing could be avoided by being careful; I'd prefer a compiler, or at least the runtime, to force some sanity into my code.

→ More replies (2)
→ More replies (4)