Top
Best
New

Posted by thejoeflow 3 days ago

PHP's Oddities(flowtwo.io)
60 points | 66 commentspage 2
RobotToaster 2 hours ago|
How could you leave out left-associative ternary operators?
makeitdouble 4 hours ago||
To note, it is surprisingly refreshing to completely forgo instanciable classes on a modern codebase.

Phpstan deals well with type definitions, arrays are powerful enough to contain whatever needed, and functions can be stored and passed around easily enough.

chuckadams 3 hours ago|
Array shapes are still second-class citizens defined in phpdoc, with an inferior editor UX, and lack of any run-time enforcement. A proper record type for PHP with value semantics would be an ideal solution for me. Would go nicely with the pattern matching proposal that's still incubating.
skydhash 3 hours ago||
If there is one thing I love about Clojure and Common Lisp, it’s that you’re not supposed to care about the shape of a complex data if it’s external to the module. What really matters are the functions that are exported and their signature. Anything that’s not standard in the library should be used as an opaque blob. And something like CLOS is about making this easier to define.
love2read 4 hours ago||
I’d love to see a post like this for JS that actually talks about things people run into. Usually when people make a post like this in js, its about archaic things nobody actually uses.
dylan604 3 hours ago|
https://www.destroyallsoftware.com/talks/wat

Archaic maybe, but still brings a smile

ceejayoz 4 hours ago||
Every time I work in another language I miss PHP’s arrays.
conceptme 4 hours ago||
Basically every other language has the same functionality (or better) as a hashmap.
ceejayoz 4 hours ago||
I’m well aware of them. I’m not sure I agree with “better”.
xp84 3 hours ago||
I’m very curious to hear your take on this. I started out on PHP for about 4-5 years then moved on to Ruby and JS. I never once had any fond thoughts for PHP’s split-personality array thing. So I’m very curious what it is that other people appreciate about it.
ceejayoz 3 hours ago||
Having to switch implementations and potentially functionality because, say, the order of the collection matters now is rather annoying, IMO.
lazka 3 hours ago|||
That array keys are auto-coerced to integers has bit me multiple times.
rokkamokka 4 hours ago|||
They are an incredibly versatile tool for sure. Even more so wrapped in a Laravel Collection
bakugo 4 hours ago|||
For me, it's the exact opposite. Every time I work with PHP, I wish I could have TypeScript's properly typed arrays and dictionaries instead of the janky untyped 2-in-1 mess it actually has.
spiderfarmer 4 hours ago||
Absolutely. If you don’t know PHP arrays aren’t actually arrays, the other languages feel inferior.
nasretdinov 4 hours ago||
if("0") {} being equivalent to if(false) {} still gives me nightmares even though I've stopped using PHP for at least 6 years now :)
moritzwarhier 4 hours ago||
I knew this in and out, but as a Full-Stack PHP/Symfony/Frontend/JS guy who pivoted to mainly TS for b2b stuff, I still have to occasionally enter

  !""

into the browser console just to be sure, during code reviews :D
tredre3 3 hours ago||
In JS/TS:

    "0" == false : true
    ""  == false : true
    " " == false : true
    "1" == false : false

    !"0" : false
    !"" : true
    !" " : false
    !"1" : false

In PHP:

    "0" == false : true
    ""  == false : true
    " " == false : true
    "1" == false : false

    !"0" : true
    !"" : true
    !" " : false
    !"1" : false
Honestly the only way to remain sane in either, but especially if you use both, is to always use === and never use boolean logic (!) when a string could be involved.
moritzwarhier 2 hours ago||
That's what I say in code reviews as well. Same for numbers.

!someValue is useful only for:

- booleans, including optional booleans (which is why every bool flag should default to false)

- undefined, null (falsy), or object/function (truthy)

It's nice for the second variant to also cover falsy NaN or things like this, for example for forms.

I guess that's where

  !!""===false
comes from.

But it's this exact case that keeps tripping me up.

What about empty arrays?

Per my original comment, now I'd have to look up if

  ![]
is false in PHP, or just empty([]) === true

.

So yea I agree, and extend your case to PHP "arrays" (in JS,

  !![] === true
is

  true
kif 4 hours ago||
Also `empty("0") === true` is a common gotcha.
cess11 3 hours ago||
I like the arrays, they make it feel like a bit of a low level language and there are some weird tricks one can do with them. As for class property behaviour, I kind of get nervous if my properties or the constructor don't enforce default values I can then fit into downstream logic.

One of the best things with PHP is PsySH, or "Tinker" as the laravelists call it. It's not a REPL in the Common Lisp sense, but it is quite nice for an interactive programming shell. I've spent countless hours solving problems very, very quickly in it, and alongside Picolisp pil + and Elixir iex it's one of the earliest tools I install on a new system.

https://psysh.org/

The thing I miss the most is a nice concurrency story. It has become better but it's still a bit of a mess, often it's nicest to just implement workers as PHP and then implement control somewhere else, e.g. Elixir, or grab one of the application servers that are nowadays a thing in PHP.

bakugo 4 hours ago||
> This example exposes the "uninitialized" state that a property can be in, which is NOT the same as NULL. This distinction frustratingly comes up when you try to do a null check on these properties:

If you're accessing an uninitialized property or checking if a property is uninitialized, you're probably already doing something wrong.

The point of class properties with no default value is that you're supposed to set them either in the constructor, immediately after creating an instance, or via some other method that guarantees they'll have a value by the time you need to read them (such as deserialization with validation).

If you want your properties to have a default "unset" value that you can trivially check for, that's what null is for. The author doesn't make it clear whether they are aware that you can declare a nullable string and give it the default value of null, but I hope they are.

spiderfarmer 4 hours ago||
I think the “bad rep” is coming from developers that stopped developing themselves.
chuckadams 3 hours ago||
I've written PHP off and on since the .php3 extension was a thing, and I can say that PHP very much deserved the bad rap it had for some time. It's evolved beyond most of that, but a lot of that is due to the composer ecosystem making up for it while the behavior of many builtins remains beyond repair. Which is fine, every language has baggage and warts. PHP's warts are sometimes heinously ugly, and they're on full display in many legacy codebases, but modern PHP is something I actually find to be fairly pleasant to develop with, far more than Go or vanilla JavaScript.
spiderfarmer 49 minutes ago||
The most under appreciated thing about PHP is the fact that after 20+ years I can still develop in PHP and never had to learn a JS framework, Rust, Go, Ruby, Java or .NET.
esskay 3 hours ago|||
A lot of it came from the rather harmful "php a fractal of bad design" article that used to get posted everywhere despite being highly inaccurate and out of date. Thankfully its fairly rare to see someone daft enough to still try using it in a discussion. PHP's come a very long way since then.
thayne 2 hours ago||
What is inaccurate about it? Maybe it is out of date now, but when I first read it about a decade ago, when I did a fair amount of PHP development, I had personally encountered most of the issues mentioned in there.

I've heard that PHP has improved a lot since then, but I don't see how you could really fix all the inconsistencies, global state, and "oddities" without a lot of breaking changes and really making it into a different language.

esskay 21 minutes ago|||
> What is inaccurate about it?

It's been debunked so many times over the years, I'm afraid I don't have the energy or desire to do it again when it's really not needed if you're far out of the php ecosystem that it really won't make a difference. Suffice to say the PHP it is talking about is nothing at all like modern PHP.

spiderfarmer 52 minutes ago|||
PHP should learn from JS where you have to abandon frameworks every two years.
fg137 3 hours ago|||
No. Bad rep comes from developers who used other languages and never looked back.
spiderfarmer 54 minutes ago||
So they stopped developing in PHP am I right?
frutjgma 3 hours ago||
I did PHP for 15 years. Modern PHP looks good but I still wouldn't go back.
phplovesong 1 hour ago||
What is even "modern php"? It has the same warts than it had back in the day, last i looked nothing is fixed.
shevy-java 4 hours ago|
All of PHP is an oddity. It is a practical oddity, but also ugly to no ends. I am glad to have abandoned it many years ago.

Surprisingly enough, I was more productive in PHP than I was in perl. Perhaps perl is even stranger than PHP.

acomjean 3 hours ago|
Modern PHP is pretty good. I’ve been using it for a while so it’s comfortable.

I’ve been doing so Perl maintenance lately and I miss PHP. Perl is a lot weirder than PHP. If I didn’t know C or had dabbled in Perl before I would be completely confused. There is More Than One Way to do it (the Perl rallying cry) causes a lot of confusion. The one nice thing about Perl is that it doesn’t really change anymore, and you can see where it positively influenced others.

More comments...