Live data from Hacker News

Modern PHP Cheat Sheet

front-line-php.com

81–90 of 127 posts

Re: Modern PHP Cheat Sheet

#81
post #37

Having used PHP since version 4 (20 years ago), I can say with confidence that its had its peak. Everything new added since 7.4 is just fluff or bloat. Fixing long standing bugs and performance improvements is fine, of course. But everything new they introduce adds to the pile of warts that's PHP. I highly regard those that try to improve it, but with the current direction it'll remain the colloquial example of how n…

I found that php8 has brought numerous features that will formalize things I am currently doing as hacks and workarounds.

Exceptions as expressions--I can stop writing, `$x = $context['x'] ?? ThrowWrapper::missingValue('x');`

Named Arguments -- I use a hacky diy `Reflection` heavy version of this for an RMI/dispatch where json key-vals mapped to function params.

Static return type and union types -- I have been relying on PhpStorm docstring hacks for this sort of type-hinting.

`readonly` will be very helpful to express performant, immutable classes. I have a number of classes that are immutable-by-convention, trusting the caller not to monkey with the internals. (The idiomatic way to do this, `$immutable->getAttr()` is too costly in tight loops compared to `$immutable->attr`)

PHP still has lots of warts (function calls why you so aspensive?!?!?) but I keep being pleasantly surprised by how good of a tool it's evolving into.

Re: Modern PHP Cheat Sheet

#82

I'm using Laravel/PHP for a new project and it's been a joy . A true joy. After spending the last 6+ years writing backend services in Node, PHP feels like a breath of fresh air. No restarting the server, no compiling errors from babel/typescript wackiness, no blocking threads, server-side templates out-of-the-box, and so much more. PHP has come a really long way, I really hope to see it's resurgence some day after e…

If you really want things to happen fast, you can also take a look at api platform:

https://api-platform.com/

Re: Modern PHP Cheat Sheet

#83
post #32

Earlier quoted context omitted.

It's so nice to see a comment giving PHP the praise it deserves. Honestly, PHP is a good, if not great, language to use for building solid web apps fast. People seem to love to hate it because it's the "cool" thing to do. Yet PHP developers continue to ship things faster, while JS devs are probably still fiddling with their Node environment setup...

> People seem to love to hate it because it's the "cool" thing to do. While I agree with this, I think the tone is slightly dismissive. From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, etc. in its history. Though I have (thankfully) seen that the language and its standard library has undergone several…

> From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance,

As an older person in the trade, I don't agree with the other older persons. Only consistency is a problem with PHP code, and that is largely NOT caused by PHP. Inconsistency is the product of developers coding without any real thought about how to abstract things (ok, well, maybe not in the case of magic strings). You can do this with every language. Some languages do everything possible to force consistency, and developer still find a way to be delightfully inconsistent even with the syntactic equivilent of bowling gutter bumpers (see Cobol, Go, Java). Consistency is a developer problem. You can write great code in languages that are terrible.

PHP has never been slow (compared to other dynamic languages), or lacked functionality. If there is a rub on PHP it comes from no separation of code and presentation, which flew in the face of the MVC pattern which was THE WAY for a very long time. Now... well, JSX much? Anyhow, my team works with Go, JS, PHP and Python, and the code that takes the least effort to both extend and maintain is... the PHP.

Re: Modern PHP Cheat Sheet

#84

Earlier quoted context omitted.

Not only shipping things faster but testing faster, scaffolding faster, debugging faster ... the tooling for PHP is much more mature than it was 6-years ago and there are so many GOOD packages out there that haven't been forked and copied 20x over. Really happy to be working with it again, even if on the side for now.

I agree. Developer speed does not get enough coverage when comparing languages. When I work with a Node project it's such a slow dragging build process. Using PHP is like a fun superpower that lets me turn out things on timescales that are borderline miraculous to clients and management.

This might be true for the web but for CLI apps etc I don't think this holds the case.

It’s exactly 180 degrees for me. Node js just seems to be super fast. And with typescript its heavenly.

I don't make a website and working on node js is fun. In PHP it is like doing something esoteric. Just a simple argument parsing is so hard in php compared to node js. In nodejs I use meow and even without docs, I can parse cli easily. In fact, I think Rust can be more easier than PHP in many cases. I usually rewrite in rust for long term daemons. Also in php I need to install php I don't know why I should install php-pdo etc. I mean why not just allow composer etc to handle like just like node do? I am frustrated with php stdlib which seems to be fossil at the moment.

The thing I like about PHP is laravel framework where I can set up websites instantly. Node js is fragmented with adonis and many other micro-framework.

Re: Modern PHP Cheat Sheet

#85

Earlier quoted context omitted.

I agree. Developer speed does not get enough coverage when comparing languages. When I work with a Node project it's such a slow dragging build process. Using PHP is like a fun superpower that lets me turn out things on timescales that are borderline miraculous to clients and management.

This might be true for the web but for CLI apps etc I don't think this holds the case. It’s exactly 180 degrees for me. Node js just seems to be super fast. And with typescript its heavenly. I don't make a website and working on node js is fun. In PHP it is like doing something esoteric. Just a simple argument parsing is so hard in php compared to node js. In nodejs I use meow and even without docs, I can parse cli e…

Stuff like argument parsing is left to frameworks. Laravel’s Artisan command system has all sorts of built-in functionality for exactly that.

Re: Modern PHP Cheat Sheet

#86
post #78

A Reddit post from a few years ago explains pretty well why PHP has such a terrible reputation: https://www.reddit.com/r/webdev/comments/5tqoqa/web_devs_wha... PHP should have chosen to undergo a rebranding a few years ago, if you ask me. The stigma it has to deal with is probably unsurmountable and will be forever. If you think PHP, you think WordPress and Joomla, and that means "hacks and vulnerabilities"... It's a…

My personal favorite PHP wtf is this one: if (md5('240610708') == md5('QNKCDZO')) { echo "true\n"; } Though I do use php, and love many things about it.

Having been out of PHP for several years now, I assume that needs a === instead of ==? It's unfortunate that we have the need for a ===, but it's no different that JS in that regard.

Re: Modern PHP Cheat Sheet

#87
post #37

Having used PHP since version 4 (20 years ago), I can say with confidence that its had its peak. Everything new added since 7.4 is just fluff or bloat. Fixing long standing bugs and performance improvements is fine, of course. But everything new they introduce adds to the pile of warts that's PHP. I highly regard those that try to improve it, but with the current direction it'll remain the colloquial example of how n…

I'm not sure the spread operator [or any form of apply()] is a good idea in a programming language. You've just added another bit of indirection that isn't even explicit, but is based on whatever the runtime state is. This is anathema to any sort of structured programming. The inferred state is just hand waved because any mismatch is pushed, as an incidental side effect, into the next function.

Re: Modern PHP Cheat Sheet

#88
post #78

A Reddit post from a few years ago explains pretty well why PHP has such a terrible reputation: https://www.reddit.com/r/webdev/comments/5tqoqa/web_devs_wha... PHP should have chosen to undergo a rebranding a few years ago, if you ask me. The stigma it has to deal with is probably unsurmountable and will be forever. If you think PHP, you think WordPress and Joomla, and that means "hacks and vulnerabilities"... It's a…

My personal favorite PHP wtf is this one: if (md5('240610708') == md5('QNKCDZO')) { echo "true\n"; } Though I do use php, and love many things about it.

Do you know why that is?

  php > echo md5('240610708');
  0e462097431906509019562988736854

  php > echo md5('QNKCDZO');
  0e830400451993494058024219903391

Re: Modern PHP Cheat Sheet

#89
post #32

Earlier quoted context omitted.

It's so nice to see a comment giving PHP the praise it deserves. Honestly, PHP is a good, if not great, language to use for building solid web apps fast. People seem to love to hate it because it's the "cool" thing to do. Yet PHP developers continue to ship things faster, while JS devs are probably still fiddling with their Node environment setup...

> People seem to love to hate it because it's the "cool" thing to do. While I agree with this, I think the tone is slightly dismissive. From what I have seen, older people in our trade seem to recommend against PHP due to it having a terrible relationship with consistency, functionality, performance, etc. in its history. Though I have (thankfully) seen that the language and its standard library has undergone several…

>due to it having a terrible relationship with consistency, functionality

Python.

Re: Modern PHP Cheat Sheet

#90
post #47

I've never understood why some programmers like to use ligatures, especially on a cheat sheet like this, which will be used primarily by people who aren't familiar with the syntax. It's not 'a' ⇒ 1, it's 'a' => 1, just write it out the correct way. Why are you trying to be fancy?

Because you're an idiot who doesn't know how fonts work.
Post reply on HN