News for hackers. Stuff that matters.
Re:PHP7 Gains Scalar Type Hints by arenaninja on Monday March 16, 2015 @06:51PM (#9215032)
Hooray! \o/

I haven't had a use for this yet, but I'm always amazed by how PHP manages to move forward without breaking BC.

Re:PHP7 Gains Scalar Type Hints by smt88 on Tuesday March 17, 2015 @12:36AM (#9216306)
You've had a use for this ever since you started writing code. You just haven't realized it yet.
Re:PHP7 Gains Scalar Type Hints by arenaninja on Tuesday March 17, 2015 @08:02AM (#9217352)
Since I can't see the forest from the trees here, care to provide an example or two?

I've had cases where I want to only allow certain values (most often int) into certain functions (usually __construct, when looking at a primary key in a database), but in these cases I prefer to cast values. (int)$foo hasn't failed me yet, and I've internalized it as much as I've internalized running application output through htmlentities.

I can see this being extremely useful if you plan on using PHP as a general purpose language, but I never have. I'd rather jump to Java/C++/Rust if I had to do numeric calculations (things like images, real time calculations, etc.)

Re:PHP7 Gains Scalar Type Hints by smt88 on Tuesday March 17, 2015 @08:24AM (#9217419)
Let's say the constructor you mentioned is for an ORM. Let's also say you're using a database library or PHP extension that you're not 100% familiar with, or maybe it just has a bug in a new version that you just updated to.

You pull out a particular row from this library/extension, and it contains an empty string (or even null) as the value for the primary key field.

Using typecasting, you'd get a value of 0, which is wrong. It would fail silently, and you'd only discover the error when you realize you're working on a row with no data. You'd even be able to update the row silently, because "UPDATE whatever SET x = y WHERE id = 0" is valid SQL!

With primitive type hints, you'd know immediately that you're getting an empty value from your DB, and you could go straight to fixing that instead.

In the past, people have had to write a bunch of unit tests to avoid all these issues. With primitive type hints, you could just type the word "int" and be done with it.

All that said, I still say all the people that are excited about this are much better off switching to a strictly-typed language, because my example above is just the tip of the iceberg when it comes to juggling types.

Re:PHP7 Gains Scalar Type Hints by marrs on Tuesday March 17, 2015 @09:41AM (#9217792)
I take your point that this is nice syntactic sugar, but I don't like your example. You wouldn't typecast in this situation. You would check the value with is_numeric() or similar.
Re:PHP7 Gains Scalar Type Hints by tveita on Tuesday March 17, 2015 @04:46AM (#9216853)
They're pretty inconsistent about it. They tend to avoid large deliberate breaks, but you still see smaller incompatibilities.

Off the top of my head, here's a case where they changed the output of a hash function between 5.3 and 5.4, breaking it for all previous users. https://bugs.php.net/bug.php?id=60221

Re:PHP7 Gains Scalar Type Hints by babuskov on Tuesday March 17, 2015 @07:32AM (#9217270)
The only consistent thing about PHP is its inconsistency ;)

Well, at least you know what you're getting into. It's not like they promise otherwise anywhere.

Re:PHP7 Gains Scalar Type Hints by AlexMax on Monday March 16, 2015 @07:17PM (#9215169)
> I haven't had a use for this yet, but I'm always amazed by how PHP manages to move forward without breaking BC.

This. If I were a Python developer (of the language itself), I would be paying very close attention to how PHP has handled deprecation and breaking changes.

Re:PHP7 Gains Scalar Type Hints by Latty on Tuesday March 17, 2015 @08:40AM (#9217480)
Really? I see it as the exact opposite. Sure, Python added some pain from 2-3. They did so to fix the main flaws in the language and remove deprecated stuff. The language is now far better and the cost has mostly been paid. The alternative is the PHP approach where everything hangs around for ever, and everyone is using deprecated stuff, everything is named weirdly and lots of extra gunk has to be shoved in to try and make it work in a sane way because "we didn't want to break anything".

I understand why some people prefer that, and why it's the way most languages go (Java does the same thing, just with way less movement in general compared to PHP), but it means that the language gets worse over time. Eventually, if you don't do that breaking change, the language will get replaced by something that doesn't need all the cludge.

Re:PHP7 Gains Scalar Type Hints by TazeTSchnitzel on Monday March 16, 2015 @07:18PM (#9215173)
There's been an effort with PHP 7 to try and avoid a Python 2/3-style situation. The PHP 5 to 7 jump should be much smaller than from 4 to 5.
Re:PHP7 Gains Scalar Type Hints by Danack on Monday March 16, 2015 @09:12PM (#9215680)
>The PHP 5 to 7 jump should be much smaller than from 4 to 5.

And also the jump from 5.4+ to 7 is probably smaller than the jump from 5.2 to 5.3.

Although I think the PHP project does need to support versions for longer, the adoption rate of 7 is going to be quite rapid due to the low barrier of doing it, and the massive performance and language gains.

Re:PHP7 Gains Scalar Type Hints by ChikkaChiChi on Tuesday March 17, 2015 @12:11PM (#9218934)
Python 2 -> 3 did not have the luxury that PHP did with 4 -> 5. Python still worked, whereas PHP 4 was running around with its hair on fire screaming for a fire extinguisher that was PHP 5.

Bad analogy, but the point is that Python still worked for most people so the upgrade process was slow.

I think Go is going to have issues similar to Python when they make the jump to 2.0. The devs are already on record saying that BC will break.

Re:PHP7 Gains Scalar Type Hints by imakesnowflakes on Monday March 16, 2015 @11:25PM (#9216107)