10 ms·
Union Types have been accepted for PHP 8
- ilovecaching 7y agoI wonder how stock PHP is stacking up to Hack both from the dimensions of efficiency, safety, and productivity these days.
- tylerjwilk00 7y agoThe Hack HHVM JIT is typically more performant than standard PHP 7. However, that is more of an apples to oranges comparison of JIT vs Non-JIT than Lang to Lang comparison. That said, PHP 7 has squeezed most of the performance from the language and the low hanging fruit is gone. However, PHP 8 looks to bring a standard JIT to PHP [1]. I'd guess that once that happens Hack / HHVM may no longer have any advantage. [1] https://hub.packtpub.com/php-8-and-7-4-to-come-with-just-in-time-jit-to-make-most-cpu-intensive-workloads-run-significantly-faster/ https://hub.packtpub.com/php-8-and-7-4-to-come-with-just-in-...
- judge2020 7y agoJust to add (you didn't say otherwise), PHP on HHVM is EoL'd https://hhvm.com/blog/2018/09/12/end-of-php-support-future-of-hack.html https://hhvm.com/blog/2018/09/12/end-of-php-support-future-o....
- tyingq 7y ago"PHP on HHVM is EoL'd" True that you shouldn't expect large existing PHP projects to run. But, the syntax is close enough that for net new projects, it's essentially PHP.
- tyingq 7y agoHack supports async in a way that's production ready: https://docs.hhvm.com/hack/asynchronous-operations/some-basics https://docs.hhvm.com/hack/asynchronous-operations/some-basi... The built in webserver, Proxygen, is also usable in production and supports TLS, http/2, etc.
- astrodev 7y agoBefore the usual avalanche of posts criticising some PHP 4 features, the union types look like a nice way to formalise something that is commonly used and very useful.
- brenden2 7y agoWhether you love it or hate it, it's cool that the PHP project has come so far. I know a lot of people still use PHP every day, so it's good to see the language continues to improve. I imagine a significant percentage of web services are still PHP based to this day.
- bpicolo 7y agoOver 30%, thanks to Wordpress.
- tooop 7y agow3techs reports that PHP is around 78% in Alexa Top 10mil sites.
- tambourine_man 7y agoFacebook, Wikipedia
- no_wizard 7y agoI believe that Facebook is fully transitioning to hacklang https://hacklang.org/ https://hacklang.org/
- 7y ago
- tylerjwilk00 7y agoIt's nice to start out typeless during problem domain discovery and proof of concept phase then quickly introduce types when you better understand the problem. This change will help that phase transition with enforcement right in the runtime as opposed to simple doc block annotations.
- ssijak 7y agoTypes are much better utilised in/while modeling domain.
- tylerjwilk00 7y agoCare to explain why types are better during domain modeling? Do you think it affects velocity? No benefits to loose types during discovery?
- k__ 7y agoIdiomatic dynamicly typed code looks rather different from idiomatic statically typed code. Dynamic is like only using untagged unions and primitives.
- tylerjwilk00 7y agoSo little meaning in that comment. Paraphrasing: 1. Dynamic and Static lang code look different 2. Dynamic lang code can use variables of undeclared type Neither statement adequately answers the parent comment.
- peterkelly 7y ago"Bad programmers worry about the code. Good programmers worry about data structures and their relationships." ― Linus Torvalds Discussion: https://news.ycombinator.com/item?id=4560334 https://news.ycombinator.com/item?id=4560334
- Supermancho 7y ago
- sedeki 7y agoIt was long ago I worked with PHP, before PHP 5. Is typing enforced with PHP these days? Or is it more like type hints, as in Python?
- chx 7y agoObjects are enforced and a simple declaration can make PHP enforce the scalars as well. https://3v4l.org/6UU2v https://3v4l.org/6UU2v https://3v4l.org/q1PAq https://3v4l.org/q1PAq In PHP 7.4 due in a few weeks covariant return types / contravariant arguments will arrive. https://stitcher.io/blog/new-in-php-74#improved-type-variance-rfc https://stitcher.io/blog/new-in-php-74#improved-type-varianc...
- sedeki 7y agoIt seems as if declare(strict_types) works for scalars as well (first example)? However, if I remove that declare from your latter example, only one output is shown. Edit: Nevermind, I get it.
- pilif 7y agoFor classes it's enforced since PHP 5. For scalars you get to chose between enforcement or type coercion for callers on a per-file basis. However, as with other dynamically typed and interpreted languages, all of this is happening at run-time, so one of the biggest benefits of strong typing (type checking at compile time) doesn't apply.
- weberc2 7y agoYou can statically type check a dynamically typed, interpreted language. Python via Mypy and Typescript being two examples.
- j_jochem 7y agoUnfortunately, PHP doesn't ship a tool for this (and it’s hard due to the way autoloading works). So PHP's typing is slightly less useful than e.g. TypeScript.
- 6gvONxR4sf7o 7y agoThis is cool! I wish tools like postgres had them too.
- weberc2 7y agoDo any relational databases have them? I’ve never understood why not. They go through the trouble of providing a mechanism to formally model your data schema, but if your data involves things that can be one thing or another, they totally punt and ask you to find a way to hack it on top via ORMs or similar. Why not model everything in Postgres such that users can issue requests against their actual data model and Postgres can make it fast, instead of building a shim layer between the data model surface and Postgres (shim layer = ORM, in case it wasn’t obvious) that can only generate queries that are likely more difficult for Postgres to optimize for lack of missing type information.
- normaljoe 7y agoI don't understand this comment from the standpoint that DBs including PostgreSQL, my fav, are not duck typed like higher level languages. They have to store the data in binary that must conform to something that can both be validated and indexed. That's to say how do you index something such as an int against a float. And how do you validate a constraint the the value must be between 1 and 10. This is why DBs can be hard and you have things like collations even among the same encoding for strings. Let DBs be good at data. I don't see the need for abstraction either especially with Postgres which has a very good type casting system. I can insert a float into an integer column without any trouble and can return the same. Postgres even has a great syntactical sugar of :: for casting such that value::int or value::numeric just works. Finally if this is a true requirement Postgres fully supports domains which are custom data types. They are not difficult to deal with and could provide for a syntax which would handle that. You might need a little work but I can have a domain which would include the int type and float type as a single data type. This still requires some plumbing to create a true union however it's not that far off. The downside however is again the DB has to store in binary so a domain like that would have two values stored for a single value which would be less than optimal solution. The true value for PHP programmers here is that we can get closer to type validation in a duck typed system. This provides multiple type validation of scalars as parameter to a function. Less interesting to Java, Python, Ruby developers where everything is an object except in a few cases. Scalars are still widely used in PHP and this feature allows for not sending "banana" as a parameter to a function, that in the past would just cast this to 1. If you think banana == 1 then you are going to have logic bugs.
- yagodragon 7y agoWhen I started programming a few years back, I was told that PHP is legacy tech and a terrible language I should avoid like a plague. After years of battling with Node.js on the backend, I took a closer look at modern PHP and the Laravel framework and I was amazed by how good the developer experience was. The language itself feels like a lite version of Java. I believe everyone starting on web development should know PHP. It's hugely deployed, it has a very mature and rich ecosystem and it's a great language to build your side project/business without spending your time on meaningless tasks. Don't be driven by FOMO like I was, there's no such thing as a "perfect programming language". Every language has it's quirks and that's fine. PHP might be old and boring but it helps you get things done faster than using the new coolest language.
- clairity 7y agophp is definitely better these days (i used to use it early in my career), but unless you're already knee-deep in php, i can't think of a web project where php/laravel would generally be a better choice than ruby/rails. alas, ruby has become passé too, in favor of javascript, a decidedly unrefined language. javascript has too many hidden ways to shoot yourself in the foot, and just hasn't evolved for developer happiness the way php and ruby have.
- sowhatquestion 7y agoAs a Laravel dev who almost went down the Rails path, I’m curious: what advantages do you think Rails has over Laravel at this point?
- tylerjwilk00 7y agoDefinitely not performance [1]. Maybe aesthetics? [1] https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/php-yarv.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
- todotask 7y ago
- baby 7y agoNow please do this for Golang :)
- Supermancho 7y agoThis is one bad feature among many good decisions, but bad nonetheless. The language will have yet-another-way to do type-erasure/autoboxing that will have to be tracked down, in the interest of removing boilerplate when multiple types are desired to be coerced. Type safety is recognized as valuable by the PHP core voters on one hand, and waved away by the other. Imagine the fun! <?php function callit(float $numI) { $numB = $numI || ($numI/2); echo gettype($numB)."\n"; $numF = ($numI/2); echo gettype($numF)."\n"; return some($numB); } function someType(int|float $numX):int { echo gettype($numX)."\n"; return $numX; } $ret = callit(2); echo gettype($ret)."\n";
- robotron 7y agoThat's why you do this instead: <?php declare(strict_types=1); Your code would flag an error in the IDE and throw a TypeError exception.