5 ms·
After over two decades of working in PHP, I'm now working in Java. PHP is basically Java-lite. I am absolutely loving the compile-time safety of Java, but I dea
by mfonda 5mo ago
After over two decades of working in PHP, I'm now working in Java. PHP is basically Java-lite. I am absolutely loving the compile-time safety of Java, but I dearly miss PHP's maps and arrays. In Java, the amount of verbosity for defining a map/list and operating on it is overwhelming.
Modern PHP is great. Many powerful language features, excellent performance, great community and package ecosystem, and decent enough safety with modern static analysis tools.
I'm not too sure I agree with the author's complaints here. When using something like array_filter, you're typically mapping from collection to collection (i.e. you don't care about the first element--you care about the whole thing) and so this problem is really a non-issue. The next follow up step would usually be foreach, or another operation like array_map, in which case it's a non-issue.
If you really do need the first element, you can use array_first. And if you really do need a fixed-sized collection, you can use SplFixedArray.
The point on properties is valid to an extent, but IMO not really an issue you commonly run into in the real world (regardless of language, your constructors should generally return an object in a usable state).
- shevy-java 5mo ago> Modern PHP is great. Many powerful language features, excellent performance, great community and package ecosystem I heard this a long time ago about perl. CPAN is great. Well ... perl entered the fossilized era. I think people do not really observe things correctly. I am noticing the same with ruby right now - everyone sees that ruby is in decline, very strongly so, in the last 3 years. Yet you have blog posts such as "ruby is not dying - it is aging like fine wine". And these are all NOT BASED ON FACTUAL ANALYSIS. I still think ruby is a great language, but if people are not realistic in their assessment of a situation, what does this tell us about people's evaluation in general? People seem to shy away from criticism. You can see this on reddit too, where moderators ban and censor willy-nilly, or even on github, where you can also quickly get eliminated for not conforming to xyz. It's as if some people are very afraid of strong opinions. I don't understand why - an opinion that is objectively false, can be shown to be false.
- greybeard69 5mo agoMate, not to be rude but your entire comment isn't based on factual analysis; it's a rant about unrelated languages.
- customguy 5mo ago> People seem to shy away from criticism. What actual criticism of PHP is anyone shying away from? PHP got bashed for such a long time, while simply nothing steps up to do what it does better. Something that, for example, is available on every webhost you can just throw files at, where all (meaningful) config and state can be in those files.
- xp84 5mo agoI used to really love the dead-simple ease PHP brought to server-side dynamic web stuff too. But when shared cpanel type hosting was orders of magnitude cheaper than anything else, that was a way bigger deal. Today you can deploy a node.js app (all the same “just a script” advantages of PHP) to a half dozen places for free, and for the next step up, a smallish instance at Hetzner, DigitalOcean or whatever, where you can just run any arbitrary container, costs less than those shared hosting once did. Why do I bring up containers? Because part of why PHP was so dope in this way was the way you can just define 1 file per endpoint and drop it in public_html, and have no server setup to do. Running say, Rails or ASP.NET or a Java site back then meant doing… a lot more, to your server. But with Docker, you can just steal a good Dockerfile template from someone else, and it’s just like 3-4 simple files for you to manage for a simple Sinatra (Ruby) or node.js version of the “one-off PHP file” things.
- customguy 4mo agoBut I don't want to manage 3-4 files, I want to manage zero files. I don't want half a dozen hosts, I want hundreds of thousands. It's not about costs, I really mean the simplicity and pervasiveness. PHP apps that are simple (in that they don't require any "rare" modules to be enabled) can easily be written to not run in relative folder structures, you can move them around like .exe files if you will. Not "like moving an exe file and then just updating a few lines in this file over there", that is a completely different thing for me. edit: Granted, I agree that if you want to do all sorts of things on the internet, maybe PHP is not the right choice. But for simple, dynamic web things that I want to just make and then run like this forever, that I can work on but don't have to? PHP and vanilla HTML and Javascript are where it's at for me, hands down. Everything else I know is either too new or seems to have constant churn or issues. That you hear nothing about PHP other than complaining it's "outdated" or whatever from the outside -- always "why are you using this?" never "why oh why am I using this?" -- is because it just hums along, IMO. I like it better than Python, and I kinda view it as in that class.
- mdavid626 5mo agoIf I have an “array” and can do array[0] to get first item, but when I filter this array and array[0] throws an error, that’s super weird. What is the meaning of [] or what is an array even? The language forces me to understand how it is implemented under the hood. That’s exactly what the author says: leaky abstraction.
- mfonda 5mo agoAn “array” in PHP is an ordered map.
- pavel_lishin 4mo agoIsn't exactly their complaint? It's called an array, referred to consistently everywhere as an array, but it just ... isn't.
- postalrat 4mo agoBetter than calling it a hash.
- pavel_lishin 4mo agoI don't think it is, tbh. Perl's hashes are a complete mystery to me still, but at least it lets me know that it's not just a linear, uh, well, array.
- duskwuff 4mo ago> Perl's hashes are a complete mystery to me still They're unordered mappings from strings to arbitrary values ("scalars" in Perl jargon). In this sense they're just like an object in JavaScript. Where this gets a little weird is that Perl arrays and hashes are fundamental types distinct from scalars - you can't put a hash into a $variable without taking a reference to it first, for instance. But that's more a matter of Perl being picker about the value/reference distinction than a hash-specific thing.
- Onavo 4mo agoHave you tried Lua or one of its variants?
- lofaszvanitt 4mo agoAll PHP needs is python's flexible and convenient manipulation of lists/dicts/objects. Plus dropping the end semi colon and it would riiiiiiiiiiiiiiiip the fabric of spacetime.
- dalemhurley 4mo agoThe semi colon allows you to have lots of flexibility in how you present your code, that is if you still read code, I seem to be an oddity now.
- chuckadams 4mo agoI'd really like it if PHP switched to Perl's grammar rule where semicolon is a statement separator rather than a terminator. It already does this in a fashion within <?php ?> tags in that statements in those don't have to end with a semicolon. I think it just blindly inserts a semicolon, but that does work -- if PHP did that whenever it saw a `}` token, it should have the same effect.
- calvinmorrison 4mo agoyes I say this now very often, that PHP has morphed into runtime Java. Quite nice in some ways. In PHP though the STDlib is not very well thought out, it may be fun(needle,haystack) or fun(haystack,needle) and you just have to remember empty() will do weird stuff like a string with the value "0" is also empty, so not great for parsing things. A lot of footguns and the best way to avoid that is with a decent linter that lets you be picky another thing I mention is avoiding the array_ stuff because of aformentioned reasons, it's easier to remember/reason about boring loops unfortunately.
- dalemhurley 4mo agoI have worked with countless programming languages and they all have little oddities.
- 9dev 4mo agoWhy not just use a proper code editor with inlay hints, inline documentation, and autocompletion instead? These things are a non-issue, unless you’re working with Notepad++ or something.
- hparadiz 4mo agoYea VSCode or Eclipse does this.
- kaptainscarlet 4mo agoPhp has compile time safety too but compile time occurs at roughly the same time as runtime lol
- blanched 4mo agoI'm not familiar with PHP, can you elaborate on what you mean here? What is it compiling? Or are you referring to type safety?
- yurishimo 4mo agoWhen a php file is loaded at runtime, it runs through a very basic JIT compiler that does statically check a few things before continuing with execution. Syntax, for example, is checked for the entire file during this step. Most type checking happens at runtime (this might not be true for interfaces at some level, but I can’t say for 100% certain - I just know I tend to see interface related errors earlier during code execution…). It’s perfectly valid syntax to declare a private method as returning an integer and then for the body of the method to return a string (explicitly cast as a string even). As long as you never call that method at runtime, no exceptions will be thrown. With a half decent IDE or LSP, these sorts of runtime exceptions can be easily avoided but technically they still exist and if you don’t know about that, it can be argued to be confusing. PHP has made a lot of trade-offs to largely maintain backwards compatibility and many of them live in decisions that happen at runtime. Modern PHP tooling can provide type safety in a very similar way to Typescript if you’re willing to put in the effort while also still technically offering you an escape hatch to do whatever the heck you want and duck type to your hearts content.
- chuckadams 4mo agoOne form of type checking does happen at compile time (which is really load time in PHP, but close enough), namely when a class extends another class or implements an interface: the types of every method and property are checked to ensure that they are substitutable per the standard variance rules (return types are covariant, parameters are contravariant, props are invariant). Everything else is checked at runtime though, and statically analyzing any of those is left to external tools like phpstan, psalm, or mago.
- flossly 4mo agoHave you tried Kotlin? It's a less clunky Java. The syntax is IMHO Ruby-level charming (for an OO-first lang), but with types that are quite a bit stronger than Java. Java interop is quite smooth.
- pdpi 4mo agoThe comparison with Ruby is spot on. I always thought that DSLs were the one thing Ruby did better than the competition, but Kotlin's combination of receiver lambdas plus syntactic sugar for calling higher-order functions make it an even better language to write DSLs in.
- flossly 4mo agoThat's exactly how i feel about it. And the code I'm looking at now with Kotlin is so similar to code i liked reading when I was in a committed relationship with Ruby.
- somat 4mo agoMaps should be a first class type in every language. I have this thing I want to do in C. Now my C is very weak so my plan was to do a prototype in python, keeping in mind the C ecosystem then sit down with my old copy of "The C Programming Language" and struggle through it. Doing it with no dicts was rough. I am normally the sort to avoid adding any libraries I don't have to but if anyone has any hints to simple hash maps in C I am all ears.
- krapp 4mo agoMy suggestions, none of which are particularly simple but it's C, you get what you get with a language that doesn't even know what strings and arrays are: 0) use Lua with LuaJIT. It has very good C interop and native hashmaps. Downside - Lua has its own rough edges and LuaJIT is frozen at Lua 5.1 with some extensions for 5,2 1) SDL3 has a hashmap implementation with its properties API, and a general purpose hashmap that they're probably going to make public at some point maybe? Downside - overkill if you don't actually want to use the rest of the library. 2) https://github.com/tidwall/hashmap.c https://github.com/tidwall/hashmap.c this seems to work fine and is the simplest hashmap implementation for C that I've seen. Downside - you still have to write a lot of boilerplate.
- alaaalawi 4mo agoyou may try https://github.com/nothings/stb/blob/master/stb_ds.h https://github.com/nothings/stb/blob/master/stb_ds.h. a single header implementation for both dynamic array and string based hash table
- thayne 4mo agoMy first recommendation is don't use C if you can avoid it. Rust, go, zig, nim, odin, c++ etc. have hashmaps built in to the standar library or the language itself, and will have other advantages over c as well. If for some reason you absolutely need to use c, consider if you really need a hashmap. If your collection is relatively small, just use an array or linked list with linear search. That's a pattern I've seen used in several c codebases, I think because of the difficulty of using map types. If linear search would be too slow, then a binary search tree is relatively easy to implement, and gives you log(n) lookup time (as long as it's balanced). Or if you build it up once and don't modify it very much afterwards, you can use a sorted array, with a binary search. If you really need a hashmap, there are some implementations, but I've also seen a few c projects that just implement their own hashmaps.
- nirui 4mo agoIf I remember it correctly, the "Java-lite" part comes rather late. PHP was more close to Perl and/or other old-days scripting language, it allows you to quickly launch a web page. Just <p><?php echo(htmlspecialchars($_GET['user'])); ?></p> and you get a hello page with a parameter specifiable via `?user=` query. But then people started to actually use it to build big sites, `echo($_GET['user'])` alone is not enough, it has to be: <?php $user = "guest"; if (!empty($_GET['user'])) { // Have to remember to do this check everytime when handling $_GET/$_POST etc ... safety check etc etc $user = htmlspecialchars($_GET['user'], ...more parameters...); } ... So people started to add module/components as well as ways to load and use those components, to enable them to write code like: <?php use My\Beautiful\InputFilter; use My\Beautiful\InputFilters\Integer; function get_page_num_from_query(InputFilter $f, array $source, string $name): bool { return $f->is_valid(new Integer(0, 100), $source, $name); } // Or something like that, hasn't write a single line since PHP5. That's when it got it's Java look.
- hparadiz 4mo agoNamespaces were added long after that step.
- DonHopkins 4mo ago>That's when it got it's Java look. Nice Java burn! But now days all you have to say to burn Java is "Lawnmower".
- egeozcan 4mo ago> If you really do need the first element, you can use array_first That probably needs an array_second too, doesn't it? Maybe array_second_from_last as well?
- larsnystrom 4mo agoJust use array_values() and suddenly you can use int indicies again.
- pavel_lishin 4mo agoThat's foolish. It would clearly be named `second_array_element`.
- hexciple 4mo agoJust had a bug the other day where we were array_filter and then feeding the results into json_encode. If you feed a sparse array into that, you get an object, not an array, which can then cause JavaScript Problems. I wasn't aware of either of those behaviors going into that debugging session.
- nsonha 4mo agoIsn't kotlin a more modern choice coming from java, that's designed propperly instead a series of bolted on decisions?