123 points · by jpswade · 5y ago · 80 comments
When I built a small, internal tool a few years ago, I couldn't believe it still didn't offer something as basic as an enum type.
Didn't bother me when I learned PHP4 as a kid, but studying CS and becoming a professional developer really changes what one expects from a language.
Until Python 3.4, there were no enums blessed in the reference implementation, but that was fine for me. Is https://docs.python.org/3/library/enum.html really that much better than the examples in https://web.archive.org/web/20120207145701/https://norvig.co... ? More powerful, sure. But if we just wanted simple C++ enums and not structured Java-like enums it's hard to argue against the conciseness of RED,GREEN,BLUE = range(3) and moving on with what matters.
I guess two advantages of having something blessed by the language are 1) you don't need to think anymore or argue about whose workarounds (if any) are better, like the sibling's comment using PHP interfaces with consts inside, and 2) if the language isn't flexible enough to add the thing yourself, if the thing needs privileged status by the compiler or whatever, then you need the blessing for the best experience. My favorite language (Lisp) has no enums, but it's flexible enough to add them (and people have done so) in a way pretty indistinguishable from if they were there from the start, or of course you can use moral equivalents to the Python 2 examples without fuss. (Rather than just variables though there are also keywords or just plain symbols, which you can have type checked with deftype, and dispatch on with object methods if you like structured enums.)
The more options a developer has the less likely he or she chooses the best solution to solve a problem. (Often best is in the eye of the beholder too, complicating things further)
One problem is that an enum can't be an index key of an array. Read why in this reddit thread between the authors of the RFC.
https://www.reddit.com/r/PHP/comments/k6w74p/rfc_enums/geo00...
Say what? Where does this instance come from and what is its scope?
> By default, Enumerated Cases have no primitive equivalent. They are simply singleton objects.
So, singletons. That way you end up with more global state, with all its consequences.
> the following features of objects are not allowed on enumerations:
> Enum/Case properties
Ok, so these are objects without properties. That could solve the global state problem but at this point you may just as well skip the entire object part as I don't see why exactly it's needed for an identity check.
Also, enumerations may have static methods but the provided example says "public function" without the static modifier.
As you point out, the lack of properties solves that. The use of a singleton probably relates to the identity issue
> at this point you may just as well skip the entire object part as I don't see why exactly it's needed for an identity check.
PHP only has 8 types (string, int, bool, float, array, null, resource, object) and they're not likely to be added to (resource is largely deprecated).
Given that, object is the only one that's not likely to be abused by relying on the actual scalar value. Keeping it as a singleton ensures that === will still work (it requires 'same instance')
There's one more thing not addressed by the RFC: how to check for a particular enum to exist. The way I see it, a new function "enum_exists" would be needed and validating a QCN would become this ugly:
class_exists($qcn) || enum_exists($qcn) || interface_exists($qcn) || trait_exists($qcn)
That links to this rfc
Hopefully ADT support should help illustrate that, I had to use F# for a long time to notice that
It’s almost as if language maintainers are removed from the practical applications that their language.
Complexity that isn’t solved in the language itself inevitably works its way into projects that use it, which is why abstractions and standard libraries exist in the first place.
Over in Java, a lot of the language changes are just syntactic sugar to make Java less verbose. Granted, Jigsaw is an exception, and JVM enhancements are good for everyone.
Structs would be great too, especially if PHP could support inner class definitions.
https://github.com/ellisgl/PHP-RFC-Struct-Data-Type
https://github.com/ellisgl/PHP-RFC-Static-Class-Type
[edit: spelling]
PHP’s main selling point has always been its low barrier to entry. Introducing high-level abstractions like ADTs will hurt that user group.
At the same time, I don’t think PHP can ever catch up with high-level languages that were built on less shaky foundations (and cleaner syntax). So what’s the end goal here?
I think we as a whole need to understand that once you get to doing PHP professional for several years the skill level and the requirements are roughly the same for most programming languages.
The company I work for is part of a large multinational company. The parent company decided to merge two of their children companies together since they operate in the same market. Sadly the two companies provide two different things in the same market and very few things overlap. When they did the merger and then COVID struck they decided they needed to get rid of developers, they took the approach that seems to be that the PHP developers were the weaker developers. So they got rid of the majority of them leaving only the ones they legally couldn't get rid of. The result, they now need to rewrite a system with only the Java and Kotlin devs who lack the fundmental business understanding of the system they need to rewrite. They also are stuck with a group of developers whose code is of a lower qualtity, that fear deploying anything at anytime on a Friday, that lack the understanding of how to build distributed systems while operating a distributed systems, who wrote systems that the day they went into production were scheduled to be rewriten because they simply weren't up to scratch with the requirements, and with a system that is labelled as scalable but can't scale as well as the system written in PHP it's meant to replace .
Trying to evaluate the needs, skills, and undestanding of developers based on what language they're currently writing in is not the wisest of moves. Evaluating a system base on the language it's written in is equally unwise.
Overall, I think PHP can compete with many languages and beat many languages in the web development area. In the areas such as data processing, concurrency, and system processes it's generally lacking due to the fact it's main selling point is building websites.
The same as people who understood functional programming, async/await/generators/ummitabilily/etc and who program in JavaScript?
JS was seen as for the lowliest of developers, but it has adopted (and people use) all those kinds of things.
Understanding is often a function of availability and/or fashion, and there's not some inherent reason PHP programmers are "dumber".
>At the same time, I don’t think PHP can ever catch up with high-level languages that were built on less shaky foundations (and cleaner syntax). So what’s the end goal here?
On one side, it already has (caught up).
On the other side, the point is moot. The end goal is that it's a very succesful language with millions of programmers and billions in projects done with it, and its team wants to keep improving it.
What did you imagine as some "end game"? Getting the node of approval from Haskellers?
While this is 100% true (though latley more true for Python), the people who started using PHP years ago and stuck with it over the years have now gotten the tools for a more precise and structured code base.
I personally use PHP even on servers for quick scripting of things because I never learned Python well enough to have all functions at my finger tips and don't get me started on powershell ;D
Also, I think a huge part of JS’s staying power was that besides its obvious warts, it always had a solid FP foundation as well. JS was one of the first mainstream languages with full closures. That was a powerful tool for building abstractions right from the start, and I don’t think Node.js would have happened without this.
Olde PHP used to declare global variables for all http get/post params, so "linter rules" basically said no undeclared globals.
Of course the linter was a sternly-worded comment in the first <? block.
Then we had the $POST array and $GLOBALS which, iirc, are considered bad-practice to interact with directly.
There are some legitimate use cases of manually loading a php file, e.g loading the auto loader, but that is usually the exception and not the rule today.
Well php has actual constants that are.. constant. So if you’re asking for an equivalent misleadingly named feature, it’d be people declaring properties with an underscore rather than just making them private.
PHP is designed that you can use the functionality you need and sub functionality to a functionality is usually also optional, e.g. you can use functions if needed and with functions you can have typed input parameters if you like, in many ways similar to TypeScript, you expand as you go.
There is nothing in PHP that forces you to code in a certain style, like in other languages, it is truly multi paradigm.
Why can’t you be a competent programmer that codes in PHP and also understands ADT? Where is that defined?
Honestly, after having used most of the languages at my disposal for past 22 years I've been doing this job - they were nothing special compared to PHP (or PHP compared to others).
You still have to figure the problem out before solving it with algorithms. No language or framework will help you do that.
I'm keen to hear why there are comments that demean one tool and elevate the other, when in reality - they're all just programming languages.
I agree, I've coded in PHP/Python/Ruby/Perl/Bash for many years. All languages have there own place if you can code!
Spend less time comparing lanuages and actually code something!
My subjective analysis is that each language has a niche that if fills nicely: - C - no BS, no runtime, close to hardware. - C++ - a bit further from the hardware, a bit closer to the developer. - Python - glue the two above with few lines of code. Go-to language for "data pipelines". - NodeJS - one code, can run both in the browser and on the server. Go-to language for web development. - Java - one syntax, runs both on Android and on servers.
- C# and Go - vendor-evangelised languages. Supported by huge vendors and their network of partners. - COBOL - too big to fail :))
I honestly don't get what niche PHP fills. Why would anyone choose PHP over another language? Developer pool? Ecosystem of libraries? Evangelized by a vendor? Front-runner in new concepts? Too big to fail?
I am not saying that you are dumb. I am saying that your values clash with my needs. This clash is so strong that I predict that if I again try to program professionally in php and its frameworks, the interaction would again have a disastrous impact on my mental health, my livelihood, and my marriage.
-----------------------------------
Let's suppose you enjoyed baking bread, but someone rejected your work and told you that gluten gives them terrible intestinal pain. You would have a choice:
1. Conclude they're being overdramatic and be offended by their rejection.
2. Believe their self assessment, move on, and continue with your craft.
-----------------------------------
(They would have a similar choice whether or not to come into a threat about baking and yell at people about gluten)
All of these languages are favorite targets of mockery by the “buzzword du jour” crowd, but they persist.
I like modern stuff, and don’t miss some of the old crud, but there’s something to be said for a language that is over a quarter-century old, and runs a considerable portion of the world’s infrastructure. I think C++ was introduced 35 years ago, and FORTRAN is about 30 years older than that.
And, of course, you have C, which is the engine behind Linux and Git.
They are working on a new COBOL standard, COBOL 202X. The last COBOL standard was 2014 which was only 6 years ago. So COBOL is still evolving
https://isotc.iso.org/livelink/livelink?func=ll&objId=194689...
Why would anyone pick PHP for a new project over, say, Python or NodeJS?
Cobol is simply kept running. Everything it does, newer languages do better. Nobody writes new systems in it.
C++ on the other hand allows you to do things that other languages simply don’t, so of course it’s still alive. New C++ projects are started every day.
A lot of actively developed languages have their niche. Python looks very elegant to mathematicians and engineers, and has fast math libraries. Java has an unmatched ecosystem. And so on.
I think PHP's niche is low barrier to entry. Maybe I'm wrong, what do you think PHP does better than other languages?
(Edit: Sorry for editing the post while you replied. I tried to word the question in a more friendly way.)