
What's new in PHP 8? - maydemir
https://stitcher.io/blog/new-in-php-8?ref=hn
======
nickjj
You know, I used PHP back in the early 2000s and sometimes I miss it after
having worked with Python, Ruby and Elixir over the years but it's not
necessarily related to the language itself.

I use this one program called Sendy (a PHP app) to help manage my email list.
It's a typical LAMP set up.

It's been running on a $5 / month server uninterrupted for 905 days with no
down time related to the app malfunctioning. The whole system uses around
300mb of memory.

Yesterday I decided to spin up a new server to finally retire the old one
since it was using Debian Jessie that just went EOL 2 weeks ago. This required
migrating a ~3 year old version of Sendy to the newest one (dozens of database
schema changes included) and export / import my entire database.

Everything just worked and all I had to do was rsync the PHP files to a
directory and access the site in a browser, and it was upgraded and ready to
go.

I even ran into what I thought was a bug because my existing forms weren't
working, so I looked into the single PHP file that was the form's action and
within 5 minutes determined I was missing a hidden field (something Sendy
added in a recent version). That was the first time I ever looked at its
source code and PHP in a long time.

It very much reminded me of my PHP cowboy days. No complicated build process,
no dependencies (the whole app uses PHP's standard library), no having to
restart the server between deploys, etc.. I know there's downsides to that and
I'm very aware of them, but after having worked with increasing complexity for
the last 10 years of web dev, man it felt nice for a change.

~~~
stephenr
Sendy is without a doubt one of the worst written pieces of software I’ve ever
had the displeasure of (a) supporting in product And (b) having to patch.

I would quit working in software dev and become a fucking gardener before I
would work on Wordpress projects all the time, and Sendy makes Wordpress look
well written.

Edit: cowboy is a very apt term for Sendy. If the cowboy were drunk, and tried
to make babies with the cows.

~~~
rumblestrut
"Sendy is without a doubt one of the worst written pieces of software I’ve
ever had the displeasure of (a) supporting in product And (b) having to
patch."

So you're saying you've never worked with Magento?

~~~
dotancohen
My thoughts exactly. Sometimes I think I hate Magento for being so overly
complex, slow, resource-intensive, and feature-poor yet bloat-rich that only
Adobe could be interested in it. Then I think of the poor people paying me to
maintain and write extensions for it.

------
sir_pepe
As someone who wrote their last lines oh PHP in something like PHP 5, this
list tickles my desire to take PHP for a spin again. Not only does the
language appear to be much improved, but also the frameworks look so
appealing! I'm seriously over dealing with all the low-level plumbing that
comes with building Node/REST SPAs just for what's essentially a bunch of
forms.

~~~
obayesshelton
Laravel is a great framework to work with, I would seriously recommend giving
it a try.

As for NPM and the JavaScript packages, this is a flipping nightmare and I
hate working with it!

~~~
azangru
I hate to state the obvious, but it's the matter of familiarity.

I've been working almost exclusively with javascript and typescript for the
last 4-5 years; so that now, while trying to write an amateurish Symfony
project (because shared hosting) and appreciating the maturity of Symfony, I
still hate almost every moment of it and hope I could have used typescript.

Anyway, what's wrong with npm and how is it so different than composer?

~~~
flamemyst
One that I can think of:

The way npm install always check latest package version and use them, even if
package lock already exists. Which broke apps build many many times due to
developer does not recognize this behavior. I know there are npm shrinkwrap
and npm ci. Its just other package manager usually follow lockfile on install
and have other command to upgrade the lockfile.

Also npm install download gazilion file for each dependencies. With deep
directories.

In the past, there are problem because npm way of installing dependencies and
hitting this infamous windows file path length limitation.

All are now already fixed or have alternative. But at that time, composer
definitely much simpler and reliable.

------
overgard
"This RFC fixes the very strange case in PHP where 0 == "foo" results in true.
There are some other edge cases like that one, and this RFC fixes them."

It's good they fixed it I suppose, but yikes. As much as people say PHP has
advanced from its fractal of awfulness days, a lot of those fixes are shocking
if you come from other languages.

~~~
kilburn
Most (non-strongly-typed?) languages have weird edge cases of this sort.
There's a very funny presentation by Gary Bernhard highlighting some of these
for Ruby/Javascript:
[https://www.destroyallsoftware.com/talks/wat](https://www.destroyallsoftware.com/talks/wat)

If you look hard enough, you'll probably find some site that shows surprising
behaviors of your favorite language. Examples:

Python:
[https://github.com/cosmologicon/pywat](https://github.com/cosmologicon/pywat)

Javascript:
[https://loomcom.com/blog/0097_the_wats_of_javascript.html](https://loomcom.com/blog/0097_the_wats_of_javascript.html)

Ruby: [https://idiosyncratic-ruby.com/29-limitations-of-
language.ht...](https://idiosyncratic-ruby.com/29-limitations-of-
language.html)

~~~
searchableguy
Are there real use cases for type coercion?

~~~
shawnz
For example, it is helpful to avoid having to do tons of explicit integer
parsing when working with the DOM, which uses strings for everything

------
mjw_byrne
I used to use PHP as a web backend and then moved to Go. Forgetting the debate
about the actual languages, it makes so much more sense to me for a web app to
be a full-fledged, self-contained program which happens to listen on a network
interface and respond when things make HTTP requests at it.

The old-school style, where routes map to scripts or parts of scripts and a
separate web server like Apache or Nginx sits in front of the app, feels
harder to reason about (does this thing persist across requests?), harder to
debug (where did that error message end up?) more brittle (did this fail
because of my script or my web server config or something else?) and more
dependent on configuration magic (i.e. various text files lying around
governing the behaviour of things).

So although some of these changes look cool, I don't think it's the language
itself that most needs improving.

~~~
sergiosgc
Funny. I completely disagree, and actually find it odd someone actually wants
desktop style application state on web apps. One of the beauties of the PHP
style of app execution is that application state is a lot simpler. Instead of
having a running application server-side, which can be in a continuum of
states, PHP forces you to start every request with a blank slate. Application
state must be explicit (with serialized server-side sessions or browser-stored
cookies). This makes it trivial to reproduce requests, trivial to debug,
trivial to scale horizontally.

To each his own, I guess. I just find your position genuinely surprising.

~~~
zapt02
By starting every request from a clean slate, you have much less mental
overhead (did I accidentally reuse a variable from a previous request?) and it
eliminates an entire class of programming bugs. It's the best thing about PHP,
and despite this approach PHP still runs faster than many similar languages.

~~~
mjw_byrne
I think the "clean slate" benefit is achievable almost for free in a monolith
though - each request is a separate invocation of a handler function which is
given the context it needs to run (e.g. an HTTP request, an output stream and
user-defined context).

The user-defined context is the only route into the application state and can
be as wide or narrow as you need. So you can share persistent state
deliberately, e.g. by having that context contain a pointer to a db conn, but
this is always a conscious, opt-in choice, and errors like accidental reuse
should be impossible unless you're doing something inadvisable like using
global variables.

~~~
fetbaffe
After doing Java Spring I realized that the Java monolith was very leaky and
inflexible. That is why you have strong trend of micro services.

In 99.9% of cases in web development you want the PHP model of doing things.
The 0.1% case is the asynchronous background job, that can be solved, maybe
not as elegant as in other languages, but why optimize the edge case?

~~~
jjeaff
My backend is written in php and I simply have a separate container running a
copy of the same php codebase that has a long running process pulling from a
pubsub queue in a loop.

So anything that needs to be processed asynchronously, like say, sending out a
few dozen emails or notifications, I just publish the task to the queue and
that other container handles them.

This could be simplified even further if you use a pubsub service that can
post data to a url as it comes in. Thus allowing the single php container to
asynchronously process the tasks all in their own individual threads.

This didn't work for me as Google cloud's pubsub seems to absolutely flood
it's target with requests seemingly ignoring 503 or any other error code and
not backing off until it has dos'ed your server.

------
richardwhiuk
> The JIT — just in time — compiler promises significant performance
> improvements ... There haven't been any accurate benchmarks done at this
> point, but they sure will come.

They wrote a new compiler, for performance reasons, and don't have any
accurate benchmarks? Really?

~~~
brendt_gd
The only benchmarks done before adding it to the core were on maths-heavy code
samples, something PHP is seldom used for.

You can read about it here: [https://stitcher.io/blog/php-
jit](https://stitcher.io/blog/php-jit)

------
zeroimpl
> Right now PHP 8 is in feature freeze, meaning no new features can be added
> anymore.

> Starting with new features, remember that PHP 8 is still in active
> development, so this list will grow over time.

Something doesn't seem right here :)

~~~
Tommah
PHP 8 adds support for quantum superpositions. (Well, it does and it doesn't.)

------
dynamite-ready
I've never quite understood the common reasons for bashing PHP. When I first
got into web development, it was effectively the only real choice available.
What it offered the beginner over the likes of Java and ASP (at that time) was
ease of setup, a coherent model for simple data driven web page development
(especially when compared to the likes of JSP), a great suite of built in
functions, and comparatively excellent documentation. If I had to teach
someone how to code now, I'd probably still consider PHP for the reasons
above. Though Python has probably eclipsed PHP for many novice friendly
reasons (docs and built ins).

------
kingkool68
I'm most excited about str_contains(), str_starts_with(), and str_ends_with().
Call me simple but I'm so glad I don't need to come up with weird work arounds
for those functions.

~~~
totony
For those who need them

EDIT: Actually, there are polyfills here:
[https://php.watch/versions/8.0/str_starts_with-
str_ends_with](https://php.watch/versions/8.0/str_starts_with-str_ends_with)

------
dgb23
I'm eagerly looking forward for union-types since I heard first of them.

PHP APIs (both core and frameworks) suffer greatly from inconsistency. Union
types in function/method signatures can greatly help static analysis. It is
great that PHP gets this built in.

------
superasn
The thing I love most about PHP is that regardless of the new features they
seldom break backward compatibility. I have a site created with PHP 5 that
runs absolutely fine with PHP 7.0 and I don't remember we had to ever update
anything in that huge codebase.

It's a pretty big achievement and avoids a lot of hassle for users as you
don't have to maintain different versions of programming languages on a
server.

~~~
john-shaffer
I've had the exact opposite experience. It's unusual for an upgrade to not
break something.

There are dozens of breaking changes in this release alone:
[https://github.com/php/php-
src/blob/96c7d42a3ca9d822a08b3dd4...](https://github.com/php/php-
src/blob/96c7d42a3ca9d822a08b3dd48060abca8240413a/UPGRADING#L20)

Some of the changes are sadistic. "The precedence of the concatenation
operator has changed relative to bitshifts and addition as well as
subtraction." Have fun debugging code that worked perfectly well for years and
now produces unpredictable results.

~~~
alganet
PHP7 will still be supported for a while. Debian will probably support it even
further. You'll have a long ass time to fix these.

Even if you wanted to push the 8.0.0 to production as soon as it came out,
you'd still have until November to run your tests on the RC branch and report
any mishaps to the development team.

The backwards compatibility is mostly targeted to major platforms (Wordpress,
Drupal, Magento, Symfony, etc).

> Have fun debugging code that worked perfectly well for years and now
> produces unpredictable results.

This describes my everyday experience with any language or codebase.

The cause for the unpredictable results are often human-related though, not
version-related.

> Some of the changes are sadistic. "The precedence of the concatenation
> operator has changed relative to bitshifts and addition as well as
> subtraction."

I would gladly remove any code that does concatenation and math/bitwise on the
same expression relying on the operator precedence. Sadistic is the person who
writes these.

More than 2000 packages were analyzed and only 5 instances of that particular
use were found[1], all bugs. This is a good change.

[1]: [https://news-web.php.net/php.internals/105442](https://news-
web.php.net/php.internals/105442)

~~~
john-shaffer
> > Have fun debugging code that worked perfectly well for years and now
> produces unpredictable results.

> This describes my everyday experience with any language or codebase.

That is horrifying. That should absolutely not be normal. I don't know what
you work with normally, but it's clear that we are working in _completely_
different worlds.

~~~
alganet
I don't know any programming context immune to human errors.

Stuff just breaks. Sometimes I get lucky and the guy before me wrote a cool
test that prevents me from having to start a debugger, but the contrary is
often the reality. OSS stuff is usually better, but not much better (OS
upgrades always break, for example).

------
obayesshelton
I would love to see PHP get some more/new love, it has a great community,
fantastic tutorials and the package manager (composer) well just WOW!

------
dpcan
PHP was just PHP for years.

Now I don’t even recognize what’s going on when reading code in a language
I’ve used for my entire software development career.

It’s like a new language.

I want to be done with tech some days.

I just want to do my job, not relearn a language I’ve used for 20 years.

~~~
tonyedgecombe
It seems every language has a group of people with an unrelenting urge to turn
it into Java.

~~~
karlerss
As long as weak typing is not abandoned, the Java-like language features are
great.

I am really looking forward to generics in PHP.

~~~
tonyedgecombe
Weak typing and generics, isn't that the worst of both worlds?

------
zaarn
My prediction is that Annotations will be a game changer for PHP-based
frameworks. Finally people will get something like in JAXR in Java, just being
able to write @@Path("/users") over their listUsers function to have it be
called when someone accesses the URL "/users" on the host.

Having to setup routing the cumbersome way has been the bane of being
productive in Laravel.

~~~
julianorinyol
Was it really cumbersome to declare a route in laravel? Isn't it just one line
in a web.php file where you associate an endpoint to a Controller function?

~~~
zaarn
The issue is that routes are declared in web.php.

In some cases, web.php routes aren't automatically updated if you use `artisan
serve`, so you have to restart that sometimes.

And of course, it means there is a lot of distance between the file in which
the controller and function are defined and the route definition. The route
definition is in "routes/web.php" and the controller will likely sit in
"app/Http/Controllers/Something.php", so if your Laravel project is moderately
sized, you'll be scrolling a bit if you don't find the editor tab.

~~~
pyr0hu
On the other hand, I don't think it's a good idea to mix the controllers with
the route definitions. How I configure or set up the routes should not be in
the controller classes, that should be a separate thing.

It's easier to see all the routes in that file instead of searching for them
in the controller, especially as you said, in a moderately sized project, I
can't be bothered to search or check the controllers. (You can counter argue
that I can just Ctrl+F it). When you open up the routes file, you can see all
your definitions in one place, which might ease the onboarding of new devs for
example.

~~~
zaarn
Well, I'd still argue it would be cleaner if they're defined _at_ the
controller (I would also say the controller does not define them, the
controller has them defined as attributes to it's class). Java devs don't seem
to have a problem handling the "look for what path this is" either, in reality
it's a grep or global search from your IDE away to be found.

The routes file for any moderately sized project will likely be sufficiently
complex that you cannot simply Ctrl-F it.

------
systematical
I like the constructor property promotion, union types, improvements to
exceptions and try/catch. Lots to like here.

Attributes worry me. Annotations are generally an eye sore to me, but in some
places could be justified. For the most part, outside of Symfony, they were
not used much. I'm worried that some developer will go bonkers with them,
create a nightmare, and that will be the next legacy code base I inherit 5
years down the line.

Named arguments is okay I suppose, but IDEs solve knowing the order of
arguments. I guess the IDEs will implement an auto complete for named
arguments? I guess thats okay, but it will require more vertical code if you
care about your lawn and keep your code in the PSR margins.

All in all I think this is a solid major, not the quantum leap we got from 5
to 7, but these are good additions. I'm sure we'll see plenty of decent
additions in the minors.

~~~
duskwuff
> Named arguments is okay I suppose, but IDEs solve knowing the order of
> arguments.

Named arguments are incredibly helpful for functions with a bunch of optional
parameters, like setcookie() -- instead of

    
    
        setcookie("cookiename", "value", 0, "", "", false, true);
    

to set httponly (the last argument), you can now skip over the unused
parameters and write:

    
    
        setcookie("cookiename", "value", httponly: true);
    

Not only is this more concise, but it's also self-documenting!

~~~
donatj
I would have personally been happy with

    
    
        setcookie("cookiename", "value", , , , , true);
    

As I used in VB.Net 15 years ago, I have no idea if other languages (C#?)
support that

------
kyleee
for some reason (probably a bad one) i love switch statements and i look
forward to the match expression

~~~
systematical
nothing wrong with switch statements.

~~~
zowanet
Coerced-typed (weak) comparisons and easy accidental fallthrough are the
problems commonly associated with switch statements.

The new match construct fixes both of these and is also an expression instead
of a statement, allowing for more expressive code.

------
NickSharp
Hooray! Named arguments finally!

------
qalmakka
After years of being a laughing stock, it's nice to see PHP finally becoming a
nicer language instead of basically being the "three languages in a
trenchcoat" of programming.

------
G4BB3R
I've used PHP for many years, but decided to stop in 2018 for my sanity.
Surprised it has basic pattern matching now, it is good for people to stop
using imperative and mutable code where it isn't needed. Anyway, since I
started to play with FP languages such as Clojure, Elixir and Elm, I am not
interested in most of OO/procedural languages anymore.

~~~
halfmatthalfcat
Any appetite to try Scala?

------
dilutedh2o
I dont see the big deal to be honest. If youre using == you're probably
approaching the conditional too sloppily to begin with

------
tpetry
When they add generics there will not be anything i really miss from other
languages when writing php. Ok tuples or simple struct types (type enforced
arrays?) would ne useful too, but phpstan/psalm can solve this problem too

------
mekster
What is the point of "Non-capturing catches"?

These kind of features would make debugging harder as people just ignore the
actual error object and write weird common error message which could be
useless.

------
continuational
> Note that void can never be part of a union type, since it indicates "no
> return value at all".

The empty set is the identity element for set union! It seems very weird to
exclude it.

~~~
choult
But "void" is not an empty set, it is no set at all...

~~~
uryga
if we want to squint and talk about sets and functions from sets to other
sets...

    
    
      function voidy() : void {
        return;
      }
      
      echo json_encode(voidy()); // -> "null"
    

so i'd say that for most purposes

    
    
      void ≡ {null}
    

a singleton set, more or less[1], sometimes called a "unit type". that's
because a void function does return _something_ , it's just entirely
uninteresting :) that's different from an "empty" type

    
    
      impossible ≡ {} // or ∅
    

with no values. there are no values of type `impossible`, so `foo() :
impossible` can never return _anything at all_ , like this:

    
    
      function foo() : impossible {
        while(1){}
      }
    

see mypy's `NoReturn`, Rust's `!`, Haskell's `Void`. it's used for giving a
type to functions like `exit()`

[1] kind of, because it won't let you `return null` explicitly.

------
The_rationalist
That's some huge release feature-wise.

------
watersb
Oh I read this as “What’s New in PDP-8?”

Was hoping for some awesome mods for a refrigerator-sized computer...

------
Snelius
An ancient evil is awakening

