And now it is approved and going into the PHP core. Great!
Why is this great? Because it is a great signal as to why PHP is THE BEST web development language out there. Not because it is a very useful language per se, but because these kinds of decisions from the supporting dev community show they CAN steer it in the proper directions to make it “right” for a majority that use it (properly). The PHP dev world is getting smarter and smarter and the language is evolving with that too. I love it.
The only thing that I think needs to happen is, this process for change needs to simply be quicker. But then again, quicker might not always mean smarter. I understand. Still, PHP needs to be pushed harder to move forward faster and by doing things better and smarter for those smarter devs in general. This change is one of those better smarter changes.
Does anyone know, if it will be possible to enforce strict mode globally instead of having to write the declaration in ALL files? And will an advantage of scalar type hinting mean a bit more performance, since the routines for type checking and conversion are simply bypassed? Or is it really a only matter of the subtle bugs that can nest into weakly typed code? And would this be one of the needed steps towards getting and using a JIT compiler for PHP successfully, like Hack and HHVM have?
What do you think about this new addition to PHP?
Scott
p.s. Oh, and one little thing I wish would be different is the declaration. Yeah, call me nitpicky. I am sitting here trying to remember what the declaration was and had to go looking for it.
declare(strict_types=1);
Why can’t it be
declare(strict_mode=1);
We aren’t talking all types and everywhere a “mode” is spoken of. So, to me, it is semantically better and thus, for me, much easier to remember.
Yes, among other things. Anthony Ferrara himself said he’d build a compiler to a lower language if this passes - it would become much easier to do. As for global mode, there is none, no. But you should be using your IDE to set stuff like that up anyway - defining a “new php file” template with a declare block on top will make sure all have it. It’s imperative to have this mode local to each file, because of the way STH works in PHP - to support dual mode, you need to be 100% aware of which file activates it and when, otherwise some people might be met with unpleasant surprises in certain projects.
So yeah, more speed, more safety, more robustness, better documentation - the whole nine yards. Yay : )
Yeah, but my personal thinking is, if we are heading towards a JIT compiler and strict(er) typing is the key to getting it, then the environment should enforce it (just like Hack does). I understand the current solution and the backwards compatibility issues and making the majority as happy as possible and all, but for those devs who are looking for the most power from the language up front, adding in the declaration in each file is just a PITA. I guess, we’ll be coming back to this later, when there is a JIT compiler to actually balk at too. I can’t wait. (and yes, I meant to write balk, in reference to my “these things need to happen faster” comment above )
Looking at that, strict mode seems rather redundant. Imho, foo(‘123’); should be allowed if $arg is int with the caveat that it’s actually converted to an int when used inside the function. Seems pointless forcing the calling code to do a conversion when it can be done automatically… as long as an error occurs when an invalid conversion such as “one” passed as an int takes place.
Now we just need array type hinting:
function foo(string[] $array) {
}
Of course that causes problems with associative arrays…
function foo(int:string[] $array) {
}
function foo(string:\StdClass[] $array) {
}
I can see why they haven’t even contemplated that yet!
edit: Is there any way to type hint generic object in this situation?
It’s autoconverted in weak mode, not converted and throws an error in strict mode, and completely optional is type hint is not present at all. It’s the best of all worlds, the only possible compromise which let it win by the required 2/3rds majority.
Array hinting would be nice, but this is already a huge, huge step forward.
Yeah, ok. Now that I think about it, you are right.
Could it be the way arrays are handled in PHP could also be a place for serious “attack” to improve? Or would you say PHP arrays are great as they are? I keep hearing how fantastic Python’s “list” type is and how it is so much easier to work with.
I’m not familiar with Python but type hinting array key/value types has the same advantages of any type hinting, consistency and enforced robustness.
I showed a couple of examples of how it could be done but maybe something like:
function foo([string => int] $array) {
}
As you could easily extend it to handle 2d arrays:
function foo([string => [int => string]] $array) {
}
Gets a bit messy, mind.
edit: To answer your question a bit more in depth… I think PHP arrays work well… the fact that we have an array and don’t need to differentiate between lists, maps, associative arrays, sets and other collections makes PHP a hell of a lot more usable than say Java where you have to learn about all the different collection types and when to use each. I love the fact that PHP gives us list operations (pop, shift) on arrays… and maps… and everything else. In a way it is a poor separation of concerns and type hinting specific types would follow the principle of least knowledge better, but as a data structure PHP arrays are wonderfully flexible. In Java you often end up converting between collection types or converting to standard arrays. The fact that PHP doesn’t need any of this makes it a lot simpler and easier to learn.
Thanks Tom. I was thinking the same about PHP arrays, but admittedly I am not the expert about other languages (or even PHP) to make that kind of call. And yes, the type hinting for array values and keys could get funky quite quickly.
I’ve worked with a bit with Java and I found the different collection types/ interfaces to be a bit…peculiar… at first and if you asked me why they are there, I still couldn’t tell you. LOL!