3 ms·
You can write PHP with generics via docblocks that are understood by PhpStorm, the two leading static analyzers (PHPStan and Psalm), and soon by the Intelephpen
by dabernathy89 4y ago
You can write PHP with generics via docblocks that are understood by PhpStorm, the two leading static analyzers (PHPStan and Psalm), and soon by the Intelephpense plugin for VS Code. Since the main advantage for generics in a language like PHP isn't at runtime, this is pretty good support.
Outside of generics, what else are you missing right now in PHP's type system?
> PHP is fast compared to what PHP used to be, but PHP is not fast compared to compiled, optimizing languages
The real question is "is PHP fast enough", and for the vast majority of use cases - including many you might not realize - it is.
- sshine 4y ago> You can write PHP with generics via docblocks In other words: PHP almost has generics. If you lock yourself to an editor that makes a MacBook Pro 2020 seem weak. These were added to Java 5 in 2004. You don't have to use a specific editor. > Outside of generics, what else are you missing right now in PHP's type system? Besides generics on functions, generics on data structures, i.e. be able to define a data structure that specifically fits a parameterised type of thing into its fields. Type inference -- you need strong typing before this is possible, and PHP's duct-tape typing isn't enough (yet). Algebraic sum types. Rust proved that you can have these without going full functional. Pattern matching. PHP is trying to get there [2], but C#'s "value types" [3] just have better semantics for getting there in a decade. PHP doesn't just suck because it misses stuff. The things that it does include are made strangely bad. Traits (type classes) -- I don't mean "PHP traits", but traits as understood in Scala or Rust, or type classes in Haskell. They're more like interfaces. PHP traits are like C#'s extension methods. Reasons why those are not so good that also apply to PHP: It's not obvious where they're defined, extending 3rd party libraries breaks encapsulation, can't be mocked. PHP programmers will pure-heartedly explore their options and make worse code by applying PHP traits in any context other when you utmost have to. Which is pretty often. > Is PHP fast enough? No. Performance is something you gradually realise you need. Just because most people don't need PHP to be a performant language, does not make PHP a performant language. You're just lowering the bar. If you make real software with PHP, because, you know, it has really grown as a language, then you'll experience performance problems sooner rather than if you'd picked a compiled, optimizing language. [1]: https://stackoverflow.com/questions/312184/disadvantages-of-extension-methods/312275#312275 https://stackoverflow.com/questions/312184/disadvantages-of-... [2]: https://wiki.php.net/rfc/pattern-matching https://wiki.php.net/rfc/pattern-matching [3]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- the_third_wave 4y ago...and still people use PHP over Scala or Rust or any of the plethora of "better" languages. Is this because all people are just uninformed, not knowing enough to be able to choose one of those "superior" languages? Nope, they use PHP because it takes the least effort to make something work. It is almost universally supported, it does not need a complicated build infrastructure - looking at you here, "modern" Javascript (among others) - and it has most of the bits needed for the average web thing included. All it takes is a single text file with a .php extension and you're off to the races while those using "superior" languages are still getting their build chain up and running. Maybe the end result using one of those "superior" languages is more performant, maybe it is easier to maintain, maybe it is less prone to breakage - all can be true. None of these are the first thing thought of when you want to get something up and running quickly and relatively effortlessly. For larger projects these things become more important and that is where those "superior" languages can actually earn that label but for a large part of the web things built PHP is just fine.