10 ms·
What's New in PHP 8.1
- Minor49er 6y agoArray unpacking is going to help make a lot of coding situations more concise. There isn't a direct comparison in the article, but from what I understand, something like this: $array = array_merge(["a" => 0], $array1, $array2); could be replaced with this: $array = ["a" => 0, ...$array1, ...$array2]; It seems small here, but anything that stops devs from having to nest a bunch of array*() calls in PHP is an excellent change. I don't know how many times I've come across code that does something like this: $array = array_values(array_unique(array_filter(array_merge($one, $two, $three))));
- reactspa 6y agoGood to have this in PHP. It's called "spread" in JavaScript.
- agumonkey 6y agoand a myriad of other names in schemes and ml dialects
- dragonwriter 6y ago...and Ruby and Python and...
- agumonkey 6y agoboth weren't in existence in the 80s
- pwdisswordfish6 6y agoAnd my axe.
- nkozyra 6y agoI always think of it as a variadic parameter, and I think spread has a little magic behind it.
- tyingq 6y ago"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.
- nkozyra 6y agoI haven't used PHP with regularity since the start of 7.0, but I agree. Having this mushy collection type seems so easy to just replace with [] for arrays/lists and {} for objects/class instantiations by convention.
- thibran 6y agoFunny. I almost dislike all of PHP but think that their choice of basic data structure is the one good thing. Computers are getting faster and faster and simplifying programming could be done to a whole new level. Clojure for example has a similar approach. Okay, PHP lacks contracts, but maybe PHP adds them in 20 years when the mainstream understands that they are great.
- kitsune_ 6y agoClojure's persistent collection types include List, Vector, Map, Set and you have namespaced keywords. In general, I don't see how you can compare the approach of Rich Hickey and Clojure to PHP to be honest...
- __jem 6y agoAlso, those collection types are backed by a number of concrete implementations.
- ashton314 6y agoI too dislike PHP, but this is one of the things I hate the most: this is like conflating addition and multiplication of types. Arrays and maps have separate use cases and very different trade-offs and PHP gives you no choice in the matter.
- iso8859-1 6y agoWhat 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.
- duckerude 6y agoThat would probably be union types[0] (or plain old dynamic typing) with checks like `is_null($val)` and `$val instanceof Type`. Offline typecheckers (not included with PHP) can analyze the control flow to prove that any invalid types have been ruled out when an operation happens. It's not nearly as principled as proper sum types, but it works alright in practice. PHP's upcoming enums are simple values, more like C's enums than Rust's enums. [0]: https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.union https://www.php.net/manual/en/language.types.declarations.ph...
- Blikkentrekker 6y agoThere 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.
- krick 6y agoThis 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.)
- cannabis_sam 6y ago
- 29athrowaway 6y agoI 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.
- jraph 6y agoDo you have evidences to back up these claims? Many PHP projects are successful. WordPress, Nextcloud, Wikipedia, Facebook... Something about it must be right.
- adevx 6y agoI think the OP has a fair point. I could be way off but I feel few developer get excited about working on PHP projects. I jumped ship to Node.js around PHP 5 and haven't enjoyed working on PHP code bases ever since. I think this in part could be due to TypeScript, giving much more confidence in refactoring and moving a project forward. The massive npm module ecosystem is probably also a major factor. The fact that WordPress and Facebook use PHP is more historic then being exemplary for it being an excellent solution.
- blahyawnblah 6y agoNone of your list says anything about the language. It says more about the companies hiring. What does PHP entail??
- 29athrowaway 6y agoErlang, Haskell and OCaml are excelent languages. But good luck assembling a team that can use those languages. e.g.: 30 developers in 1 year. And those languages are pretty popular compared to others. The programming language you use is a important factor when it comes to scaling your team (and your product, and your business). I do not want to start a bikeshedding holy war. The reference implementation of PHP is pretty inferior to that of other scripting languages such as Ruby, Python, Julia and Node. There are other implementations that are better but come at a compatibility cost.
- AdrianB1 6y agoI am not a developer, but I use PHP a lot for scripting and personal automation including small web apps with SQL backend. I found PHP 7 to be a great improvement over 5.x, but with 8 I skipped every single new thing they brought. I can use PHP 8.1 in PHP 7.4 compatibility mode, but I find the features in 8.1 making the language less readable and less logic; for example, 3 dots become part of the language syntax? What is next, emojis as operators? Secret gang hand shakes as functions?
- FriedrichN 6y agoFun fact: failure to implement unicode is the reason we never got a PHP6. So we're safe from emojis for at least a while.
- TazeTSchnitzel 6y agoPHP supports Unicode, just not in a Python 3-like way.
- deergomoo 6y ago... was already syntax, for variadic parameters. Re-using it for unpacking makes perfect sense IMO.
- Sniffnoy 6y agoWhat's wrong with ellipsis as syntax? Plenty of languages have some sort of ellipsis (either 3 dots or 2) as syntax. See for instance https://en.wikipedia.org/wiki/Ellipsis#Programming_languages https://en.wikipedia.org/wiki/Ellipsis#Programming_languages and https://en.wikipedia.org/wiki/Ellipsis_(computer_programming) https://en.wikipedia.org/wiki/Ellipsis_(computer_programming... . JavaScript/TypeScript also uses it in a similar way to PHP.
- svnpenn 6y agoI recently killed all my PHP code in favor of Go. What finally convinced me is the Go package system. I like that Go has a builtin package manager, while I dont think PHP does. I know about Composer, I just wonder. After all these years, why hasnt PHP incorporated a builtin package manager?
- nolok 6y agoYour question is weird, like a solution in search of a problem. So let me ask, what problem does composer have that you hope to see a builtin package manager solve (at the cost of losing its own dedicated team) ?
- skissane 6y agoWho knows, maybe one day Composer will be bundled with PHP. (And bundling Composer with PHP wouldn't necessarily mean that Composer would lose its own independent dev team.)
- jspaetzel 6y agoPHP developers have been very successfully using Composer for many years. It's actually quite nice having the package management decoupled from the language, there's fewer chances for breaking things between upgrades which helps slightly with long term support. Congrats on getting a package manager for Go!
- stunt 6y agoAs long as you got a strong package manager and good ecosystem around it everything is ok. Many languages don't have built-in package manager.
- eyelidlessness 6y agoI'm reflexively upvoting this because it's absurd to me it's grey even though I don't have a dog in the fight.
- ashton314 6y ago> 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 into real arrays (a linked list might be acceptable under certain circumstances) and maps.
- deleted 6y ago[deleted]
- FriedrichN 6y ago> 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.
- Shish2k 6y agoThe 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.
- FriedrichN 6y agoWhen using strict typing that is indeed a problem. Thanks for the example. edit: itchy trigger finger
- TazeTSchnitzel 6y ago> changing the behavior of your data structure based on its contents PHP does no such thing. There is one array type with one behaviour. There is no distinction between different kinds of arrays in the language.
- mike503 6y agoThis 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 JS to me now. Anyone want to start a fork called “PHP Classic”? :)
- deleted 6y ago[deleted]
- cyberCleve 6y agoAre you saying you do not use IDEs? What is your rationale for that decision?
- mike503 6y agoNever have, never got into it. Edit directly on shell. Muscle memory, creature of habit. :) also still only write procedural PHP myself.
- throwawayboise 6y agoI just use emacs. It has enough capability to be an IDE, but I don't use it like that. I use it as an editor, maybe with a language mode to do syntax highlighting and help with formatting. I tried Visual Studio years ago, didn't care for it.
- spillguard 6y agoIs 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.
- mike503 6y agoexcept more people will wind up writing "new" PHP, and the original ecosystem in which it grew will mostly be gone.
- bouncycastle 6y agoOne thing I still love about PHP is that it's so easy to deploy. No need for fancy Amazon Web Shmervices, no need for Jamstack jumping through the hoops. Just find any host that supports PHP, upload your file, and done.
- shash7 6y agoEasy to get started.. nightmare to maintain aftewards.
- raspasov 6y agoI don't use PHP anymore, but I used it for years in production. Was pretty easy to maintain (via Apache's mod_php).
- hermanradtke 6y agoIt is a problem to maintain. Simple example: how do you remove sessions in a multi server environment? Well, you can set a config to do that at random which imposes a cost for that particular request. Or you can run a corn job. This is not a decision in most other scripting languages. Don’t get me started on concurrency either...
- bawolff 6y agoWhy is this not an issue in other scripting languages? You either delete at runtime or delete in a separate process. What other options are there?
- bouncycastle 6y agoThis is a decision in all scripting languages. PHP solves concurrency very well - it just passes the problem to the web server. If you need multithreading to service a http request then you are either doing something wrong, or over-engineering the problem. Other language platforms also service requests in a single thread, including Node.
- rangoon626 6y agoWe need a very big expansion of php usage