Live data from Hacker News

What's New in PHP 8.1

stitcher.io

51–60 of 112 posts

Re: What's New in PHP 8.1

#51
post #33
post #3

"New array_is_list function" I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied some of Perl's features, and I never understood why hashmaps and arrays weren't different types, as they are in Perl.

I love them. Lisp can use lists for everything, and PHP arrays ;) Thanks to their mixed nature they're very versatile. People even make small DSLs out of them. And they're order-preserving, so they won't inject non-determinism just to smugly teach you a lesson about real hash tables.

Almost anything can be simulated in any other data type, often with poor performance and easy bugs.

If a programmer use lists in lisps where vectors or hash tables are more appropriate, he is doing something wrong.

Likewise, simulating arrays in hash tables, which is what PHP expects one to do, is a very bad idea.

Bash lacks multidimensional arrays, but it does have hash tables, so some programmers resort to simulating multidimensional arrays by using keys such as `"3,4"` as a string in hash tables, this would be a very bad idea if Bash had true multidimensional arrays. In it's lack thereof, it is only a bad idea where no better idea is possible.

Re: What's New in PHP 8.1

#52

What is the idiomatic way to have data in an enum in PHP, aka sum types? Sum types in languages like Rust or Haskell or OCaml can have data associated with them. You'll get an error before runtime if you assume a field is available when it isn't.

There is no need for sum types in a dynamically typed language that provides checking the type conditionally.

Sum types are, in effect, a small domain of dynamic typing.

Re: What's New in PHP 8.1

#53

> array_is_list This is a stupid solution to a stupid problem. Defaulting to weak typing has enough problems, but changing the behavior of your data structure based on its contents is next-level pain. I used PHP for a few years at a student dev job and I can’t count how many times this conflation wasted my time and introduced various bugs. If PHP wants to become more respectable as a PL, it has got to break that out…

> I can’t count how many times this conflation wasted my time and introduced various bugs Could you give examples of this? I'm of the opinion PHP could use more strictly typed arrays/lists, but I haven't run into any issues with PHP arrays myself.

The one that annoyed me recently - numeric-looking string keys are turned into numeric keys.

So if you have eg a hashmap of [naughty_word => replacement], then [“penis”=>”willy”] and [“asshole”=>”bottom”] work fine, but [“69”=>”cuddle”] crashes your program with an "unexpected integer” exception.

Re: What's New in PHP 8.1

#54
post #3

"New array_is_list function" I use PHP a fair amount, but this is one area that irks me. PHP arrays can be arrays, lists, and hash maps. PHP obviously copied some of Perl's features, and I never understood why hashmaps and arrays weren't different types, as they are in Perl.

I'm kinda used to PHP "arrays", but I really wish it had proper lists. This array_is_list function doesn't change things much: I already have the same implemented in many of my projects. The point is that I shouldn't do that: I just should use a typehint the same way as I'd do it with array or MyCustomClass.

Of course there is Ds\Sequence, but having it as a part of some relatively obscure library isn't the same as having it as a part of a language, with syntax as simple as [1,2,3]. Actually, I'm not even sure: does it have as good performance, as "arrays" do? I never tested.

Re: What's New in PHP 8.1

#56
post #27

This is really making what used to be an easy to read, easy to learn language a lot less of that. Some of us still don’t use IDEs and are okay with more thoroughly defined code that is readable. If people are so gung ho on adding in syntax hacks, shortcuts, etc, they seem to be bringing ideas from other languages and why not simply continue to use that other language? PHP seems to be looking more and more like Go or…

Is that what PHP should strive to be - the "most easy to read language available"? What does that even mean, just that there isn't any shorthand? No one is forced to use the new syntax, it's just different tools in the tool belt.

except more people will wind up writing "new" PHP, and the original ecosystem in which it grew will mostly be gone.

Re: What's New in PHP 8.1

#57

I do not think PHP is a good language to use in a project. - dev recruitment is hard. - dev retention is hard. - hard to find people that write clean, modern, performant PHP - it is PHP, with everything that it entails. It does not enjoy the best reputation as a language.

Most of those issues can be answered with "maybe you're not paying enough".

Re: What's New in PHP 8.1

#58

What is the idiomatic way to have data in an enum in PHP, aka sum types? Sum types in languages like Rust or Haskell or OCaml can have data associated with them. You'll get an error before runtime if you assume a field is available when it isn't.

This isn't exactly a sum type. Sum type is basically a Union, a proper algebraic data type, and PHP doesn't have algebraic data types (although, even before type hints it was a relatively common practice to "implement" them via PhpDoc... horrible, I know).

Enum is basically a bunch of constants, for all practical purposes. There were enums in Java for a long time, long before languages with more-or-less proper type systems became mainstream, and everybody was fine with a rigid mess of type system Java has.

So, to have what effectively is enum in PHP, I do just

    class Status
    {
       const STATUS_A = 'STATUS_A';
       const STATUS_B = 'STATUS_B';
       // and so on
    {
So it looks like a new enum would be basically just a syntactic sugar for that. (And finally a way to make a type hint for $a = Status::STATUS_A.)

Re: What's New in PHP 8.1

#59

> array_is_list This is a stupid solution to a stupid problem. Defaulting to weak typing has enough problems, but changing the behavior of your data structure based on its contents is next-level pain. I used PHP for a few years at a student dev job and I can’t count how many times this conflation wasted my time and introduced various bugs. If PHP wants to become more respectable as a PL, it has got to break that out…

> If PHP wants to become more respectable as a PL

Oh boy... That ship has sailed a long time ago...

Re: What's New in PHP 8.1

#60

> array_is_list This is a stupid solution to a stupid problem. Defaulting to weak typing has enough problems, but changing the behavior of your data structure based on its contents is next-level pain. I used PHP for a few years at a student dev job and I can’t count how many times this conflation wasted my time and introduced various bugs. If PHP wants to become more respectable as a PL, it has got to break that out…

Dynamic typing is a trade off, one that requires some explicit type checking in some situations.

It doesn't make it a stupid solution or a stupid problem.

If you validate and conform your types at program edges it's rare you ever really need to do type checking.

That is, a properly structured PHP application has all the benefits of dynamic typing and few of the problems that static type checking supposedly eliminates.

I like both static and dynamically typed languages.

Just like I like Async and non Async languages.

All language types require practice and learning to adjust to the different patterns that are required.

If you write PHP like JavaScript, or if you write PHP like java, or php like C++, you're going to have these problems.

If you write PHP like PHP, function heavy, class light, and a mix of functional and procedural depending on context it actually works out very well for a lot of things.

But go ahead, call the language stupid and irrespectable.

Post reply on HN