16 ms·
PHP 7.4
- RadioactiveMan 7y agoThe praise for arrow functions is surprising. They don't enable anything new and their feature - saving a half dozen characters - comes with the cognitive cost of tracking how both types of anonymous functions work and their subtle differences in behavior. It's not worth that price.
- donatj 7y agoI’m with you here. I’d rather have a single uniform syntax for anonymous function.
- dpcx 7y agoThe big thing enabled by them is not needing to `use ($variable)` to pull a variable in to the scope; however, IMHO, that actually then adds cognitive overhead to the system - I personally will stick to "fully" generated closures.
- leetbulb 7y agoThis is the correct answer. Having the option of implicit by-value scope binding is nice. However, I do agree that having the two does add to the overall cognitive cost, albeit not significantly.
- johannes1234321 7y agoThis is a matter of the situation. For a single statement this can be extremely clear, if the function becomes longer being explicit is nice. And at some length the thing should have a name. usort($data, fn($l, $r) => $this->foo($l, $r)); vs. usort($data, function($l, $r) use ($this) { return $this->foo($l, $r); }); vs. usort($data, [$this, 'foo']);
- deergomoo 7y ago> For a single statement this can be extremely clear PHP arrow functions can only contain a single statement and always implicitly return, so that sort of thing is exactly what they're designed for. Although worth noting with your second example that you haven't needed to capture $this in closures since 5.4, and in fact it is an error to do so now (though I'm not sure when that was introduced).
- johannes1234321 7y agoUsing $this was just a simple example where all three cases could easily do the same. Make it $bar :) Anyways: People are capable of writing loooooong expressions (chaining with && can be good to handle errors from multiple operations in one place (till you have to figure out which specific condition broke)) Thus "single expression" isn't neccisarily short ;) Anyways: Point is "it can be useful, but developers should think, what they are doing" Perl's "there's more than one way to do it" probably goes too far, but being able to focus on the relevant parts and being able to providing the amount of context needed are good things.
- johnchristopher 7y agoI am diving back into node lately and it's really annoying to decipher the async and promises styles that are peppered with different anonymous function styles. I too stick to the most explicit version (function($foo)). It's the same for command line arguments, I have an easier time remembering words (--preserve-permissions for instancem --volume, etc.) than letters if I don't use the interface enough.
- Cthulhu_ 7y agoThis is a problem in a lot of programming languages nowadays; they borrow features and syntax from other languages without those new features superseding others, and without providing a mechanism to force one syntax over the other. So that's why we now have half-assed OOP in Javascript, we have Scala which is basically all programming languages ever, etc. Meanwhile, Go is still fighting a lot of requests to add language features, knowing full well the complexity added to the compiler (and more importantly the cognitive overhead for developers).
- oaiey 7y agoThe art of language design is staying true with its own established patterns. C# fights this battle with primary constructors or combined patterns.
- earthboundkid 7y agoThat’s a weird choice. Arrow functions in JS don’t redefine ‘this’ like regular functions, so if you don’t use them, you waste time working around the redefinition.
- hombre_fatal 7y agoAlso, if you default to `() => ...` for anonymous functions, then you can reserve anonymous `function() { ... }` for the rare times you actually want peculiar `this`. Same reason it's nice to default to `const` so that `let` becomes a signal for the bonus feature of reassignment.
- tyingq 7y agoPretty big list of new features: https://www.php.net/manual/en/migration74.new-features.php https://www.php.net/manual/en/migration74.new-features.php Happy to see the proc_open() feature of NOT spawning a shell, and interested in the FFI implementation.
- bilekas 7y agoPraise Jesus Typed properties!!
- donatj 7y agoI’m on the fence about their usefulness. There is a type check on every write, which can lead to major performance losses. Rasmus, the creator of PHP, lamented this in a recent talk. Using getters and setters already got us type checking external to the class, and is generally good OO practice. Internally I think static analysis on documented types was a good enough solution.
- derision 7y agoLooks like they're optional anyway so you're free to do things however you like
- lowercased 7y agoisn't the system still going to have to check if a type exists or not anyway, contributing to the potential speed issue?
- derision 7y agoI'm assuming the check happens at compile time (and therefore cached in the opcache, which you should be using in production anyway)
- lowercased 7y agoi would think you'd still have dynamic check - trying to set a string in to an integer type would happen at runtime - you'd be checking before each set call. I'm not sure you'd be able to catch all the runtime potential issues ahead of time (but I've not kept up with php internals for awhile - perhaps someone's got a good way to do that now?)
- sexy_seedbox 7y agoArrow function and spread operator, that's so... ES6 :o
- theodorejb 7y agoTyped properties and arrow functions are great, but I'm also happy to see the deprecations in this release. E.g. nested ternary operators without explicit parentheses (such use was almost always a mistake). Full list here: https://www.php.net/manual/en/migration74.deprecated.php https://www.php.net/manual/en/migration74.deprecated.php. Some of the deprecations pave the way for performance improvements in PHP 8.
- nwellnhof 7y ago> nested ternary operators without explicit parentheses This also seems like a first step toward fixing associativity of the ternary operator, one of the most ridiculous deficiencies of the PHP language.
- duskwuff 7y agoThat's exactly the intent of this change. "Future Scope: We could make the ternary right-associative in a later release, after it has been an error for a while." https://wiki.php.net/rfc/ternary_associativity https://wiki.php.net/rfc/ternary_associativity
- m712 7y agoI have been waiting for a strongly typed mainstream high level language for a long time (TypeScript doesn't count because all type information is stripped during runtime). PHP still has a lot of cruft that prevents me from using it in my projects, but the recent updates seem like a step in the right direction. Hope that by PHP 8 the language will have deprecated more of the bad stuff.
- mweibel 7y agowhy not Go/Java/....? Honest question.
- tyingq 7y agoMaybe they prefer optional OO over mandatory or no OO?
- mweibel 7y agomaybe? I wouldn't know. "strongly typed mainstream high level language" does not say anything about that, hence my question.
- mschuster91 7y agoAt least for Java, PHP has the massive advantage of "change the code, save it and hit cmd+r" while Java, JavaScript and .net require recompiling, repackaging and deployment. At work I develop for both PHP and Java projects and... it's cumbersome to develop for Java projects, to say it nicely. Ever tried developing for Jira, Jenkins or enterprise CMSes?
- krapp 7y ago> while Java, JavaScript and .net require recompiling, repackaging and deployment. Not to be a pedantic curmudgeon... actually, precisely to be a pedantic curmudegon... javascript does not require any of that. The byzantine and asinine ecosystem that has sprouted like a cancer around javascript when SV and Node noticed the language and its profit potential, fueled by hype and unnecessary complexity? Yes. But the ecosystem is not the language. Javascript itself is exactly like PHP in this regard - you edit a file, upload to the server, refresh the browser. Everything else is unnecessary. Useful? Maybe. Bullshit? Probably. But not necessary.
- jonny383 7y agoTyped properties are well overdue! PHP is almost sexy again...
- mekster 7y agoOnly if it can get rid of semi colons.
- Aaronmacaron 7y agoI've been waiting so long for this day. Finally arrow functions in PHP! Until now it was extremely cumbersome to use higher-order functions such as array_map() or usort() . This is especially true if you're a full stack developer who frequently switches between PHP and JavaScript which has had support for arrow functions for a long time. The hitherto existing anonymous functions are so verbose and it's a real pain to bind all closure variables manually.
- bufferoverflow 7y agoI wish they implemented them like in JS without the unnecessary fn().
- Denvercoder9 7y agoThat would give syntactic ambiguity, since the => operator is also used to separate keys and values in array notation.
- _rs 7y agoFair, but seems inconsistent to have `fn` instead of `function` like we use for anonymous functions - unless `fn` acts like an alias to `function` now (which doesn't sound like is the case) which wouldn't be as ambiguous for the compiler to figure out
- bufferoverflow 7y agoI don't think there's an ambiguity, since the key-value application of that operator only happens inside the array definition or when you do foreach ($x as $k => $v) {}.
- TimothyBJacobs 7y agoThere is ambiguity. This was specifically mentioned in the RFC for the feature: https://wiki.php.net/rfc/arrow_functions_v2#syntax https://wiki.php.net/rfc/arrow_functions_v2#syntax
- pentium10 7y agoFor those who want to use on serverless functions the new PHP functionalities there is a way using Cloud Run.
- mikece 7y agoWhile everyone else (well, me) have their attention on the hippest, newest languages PHP has been steadily improving, getting faster, and still runs a lot of the largest web properties. It wouldn't surprise me if before long PHP can be compiled to native binary* complete with a memory efficient, non-blocking web server and performance approaching what Go and dotNetCore can achieve... and the option to emit a "normal" PHP web app that can run anywhere if you don't need Facebook-level performance from it. *I realize HipHop PHP did this already but it doesn't have it's own baked-in web server afaik. I'm thinking along the lines of compiling down to something small and fast to be deployed to K8S clusters without needing containers for Apache or nginx as part of the setup.
- tyingq 7y ago"it doesn't have it's own baked-in web server afaik" It does, actually: hhvm -m server -p 8080 Uses proxygen. Proxygen is pretty cool, actually. Even does http/2 & https, and is suitable for production use. I'm somewhat surprised nobody's done the work to link it with PHP, Perl, etc, to provide what node.js does for js. Edit: Also, on compiling PHP, there's a pretty good start here: https://github.com/ircmaxell/php-compiler/blob/master/README.md https://github.com/ircmaxell/php-compiler/blob/master/README.... The FFI functionality in PHP 7.4, plus the internal use of an AST in php7.x, are what made it possible. Missing lots of functionality, but a neat proof of concept.
- merb 7y agophp also has one: php -S localhost:8000 -t foo https://www.php.net/manual/de/features.commandline.webserver.php https://www.php.net/manual/de/features.commandline.webserver... since 5.x (not production ready tough)
- trm42 7y agoIf you use Swoole, you can implement a performant server in a async Node fashion with your code, PHP and Swoole extension: https://www.swoole.co.uk/docs/modules/swoole-http-server-doc https://www.swoole.co.uk/docs/modules/swoole-http-server-doc
- conradfr 7y agoI guess null coalescing assignment operator will save a lot of typing but I'm not sure it leads toward a more readable code, especially with ?? and ?: already part of the language.
- tyingq 7y agoI suppose alternatives like ||= or //= have their own issues.
- SergeAx 7y agoThe most interesting thing about PHP is it is, AFAIK, the most community-driven language today. Go, Python and JavaScript are all under heavy influence of Google, C#, VB.NET and TypeScript is Microsoft's turf, Java is Oracle's, Rust and WebAssembly - Mozilla's. С and С++ are holding their grounds, but they are more and more niche today. Facebook tried to domesticate PHP, but failed spectacularly. So it looks like PHP is most democratic and liberal programming language of today) I am glad it's getting steadily ahead.
- oblio 7y agoRuby? Also, why do you say that Python is controlled by Google?
- mikepurvis 7y agoProbably because they employed GvR from 2005 to 2012.
- SergeAx 7y agoSee above: Ruby is good, but it's on decline and IMO soon going to be a niche legacy language for projects too big to rewrite, like Github and Gitlab. Python was influenced by Google and that influence is hard to wear. Until now Python is definitely not community-driven. Also, look at this version 2/3 snafu.
- petters 7y agoGoogle does not care that much about Python, at least not when I was working there. I would certainly not mention it together with Go.
- Freegile 7y agoI wonder why they added the "fn()" thing on the left of the array functions. Javascript: $square = $x => $x*$x; PHP : $square = fn($x) => $x*$x; Probably so that it works inside arrays: $a=[fn($x)=>$x*2,fn($x)=>$x*$x]; echo $a[1](5); // 25 Well, it also makes it a tad more readable, so I am ok with it. Since we are talking about new features of PHP: Does anybody else wish there were named parameters? I think PHP is pretty complete. It is my favorite language and I don't really wish for anything added to it. Except for two things: 1: The named parameters of Python: def paint(what,color='blue',tool='pen',layers=1) ... paint('house',tool='brush') 2: The short object Syntax of Javascript: city={name: 'Berlin', population: 3748000}
- DCoder 7y agoTypeScript spoiled me. I want PHP to have generics and all the type-system goodness that TS brings.
- nicoburns 7y agoThat would be great. I would also love it if TS supported runtime checks like PHP.
- ludamad 7y agoMy thesis was working on top of ConcreteTypeScript, which supports runtime checks https://github.com/ludamad/LocusTypeScript https://github.com/ludamad/LocusTypeScript. My contribution was adding the ability to define types inline with code, and check those using tagging. That being said, it's all very experimental.
- deleted 7y ago[deleted]
- dschooh 7y ago> I wonder why they added the "fn()" thing on the left of the array functions. There would be ambiguity without it: https://wiki.php.net/rfc/arrow_functions_v2#syntax https://wiki.php.net/rfc/arrow_functions_v2#syntax
- arkh 7y ago> It is now possible to escape question marks in SQL queries to avoid them being interpreted as parameter placeholders. Writing ?? allows sending a single question mark to the database and e.g. use the PostgreSQL JSON key exists (?) operator. Good.
- mpartel 7y agoHaving followed and at times heavily used PHP since the 4.x times, its story seems to be that of slowly (and begrudginly?) accepting modern language features like classes, exceptions, lambdas, GC, static typing, etc., sometimes getting things wrong on the first try but still mostly converging towards modernness. That begs the question, what's PHP's value proposition for new projects? If PHP mostly chases feature parity with Java/.NET/TypeScript and doesn't attempt to do anything better/different (like Elm/Haskell/Rust/Go/Erlang arguably do), then why choose it?
- blowski 7y ago* Lots of cheap developers available * Strong community * Mature frameworks and libraries * Simple hosting model
- mpartel 7y agoFair, though I'd say 1-3 are more or less matched by Java/.NET and probably NodeJS. Not sure if you meant this, but now that I think about it, the "one process per request" model is actually legit differentiator that obviates a lot of the concurrency trouble new developers can get themselves into with other languages. It makes good performance harder in some cases (though there are workarounds), but overall it's maybe the one thing in PHP that I'd like to see other languages make some note of.
- blowski 7y agoI'd agree that there are other languages with equally strong communities and mature libraries. I would say it's easier to hire an OK PHP developer for £40K than for any other language, at least that's my experience hiring in London. The hosting model - what you said, plus not needing to think about memory leaks. There are so many Java and .NET boxes out there that need to be rebooted every 2 hours because of some insidious bug that nobody on the team can solve. It's also pretty easy to put PHP in a container, and scale horizontally.
- 7y ago
- agumonkey 7y agoAnother step in the homogeneousness of ~dynamic programming languages. PHP is now near ES6 parity, which was a ml/scheme revival.
- GrumpyNl 7y agoWhy this, can someone please explain. Numeric literals can contain underscores between digits. <?php 6.674_083e-11; // float 299_792_458; // decimal 0xCAFE_F00D; // hexadecimal 0b0101_1111; // binary ?>
- deleted 7y ago[deleted]
- RadioactiveMan 7y agoHere's the RFC for it with the "why" and discussion of the tradeoffs. https://wiki.php.net/rfc/numeric_literal_separator https://wiki.php.net/rfc/numeric_literal_separator
- samcday 7y agoThere's a lot of PHP positivity in the comments. I just want to inject some sobering pessimism in here, as someone who has spent the last 6 months using PHP for the $dayjob, and used to do commercial PHP work back in the PHP5 days. PHP .... ... Doesn't ship with a production ready webserver. Yes, Python and Ruby also don't, but they have easy tools you can reach for (gunicorn, Puma) if you need to run them in a modern deployment stack where a whole nginx isn't really necessary. ... Doesn't have basic collection types in the core language. It's 2019 and PHP still thinks it's logical for the standard language to throw all Map, Set, List semantics into a single PHP array type. You can install the "phpds" native extension (and polyfill it from Composer) to get some passably usable implementations. But coming from Golang / Rust (or hell, even ES2015) I find them frustratingly limited. ... Doesn't have any sort of threading / async support. Even in a modest-scale webapp, this is going to hurt you when you want to do things like deploy PHP as a job queue handler. Or fan out even a trivial number of HTTP-client API calls from a PHP controller in a microservice-y world. The latter can be sort-of achieved with naive hacks like the cURL client concurrency support (Symfony HTTP-client supports it: https://symfony.com/doc/current/components/http_client.html#concurrent-requests https://symfony.com/doc/current/components/http_client.html#...) but that's a hack at best. ... Doesn't ship with a debugger out of the box. Even with great tooling like phpstorm IDE, you have to actually rub braincells together to be able to get xdebug installed / configured and ready to use. ... Has a somewhat decent package manager (Composer). But it doesn't ship with the language. Also, it's kinda crappy. It's painfully slow to do a composer install/update on anything larger than a toy project. It has all sorts of weird non-determinism issues (the composer.lock file flails around wildly between runs, changing case of metadata fields, etc). ... Has semi-decent static analysis tools (Psalm, phpstan), but they're pretty garbage compared to a proper compiler, or the quality of static analysis tools in other dynamic languages like Ruby / Javascript. I could keep going. Profiling, code formatter, library ecosystem quality, and so on. Where am I going with this? Honestly, I wish PHP would just die. It has some pros, but overall it's a terrible language that has no hope of modernizing enough to be relevant in 2019 and beyond. There's plenty of other excellent languages that do a better job in any category that PHP attempts to be good at. The effort being spent to maintain PHP as a language/runtime could be better spent on other languages that have made better choices in language design. The developers who have become proficient in PHP, should continue to use their employable skills to maintain existing PHP codebases, but take a bit of time on the side to get proficient with another popular language if not already. And for the love of god, stop starting greenfields projects in PHP! Let it fade away peacefully.
- hc91 7y agoWhy is this garbage fire of a language still alive is one of the big mysteries of the universe