205 points · by brendt_gd · 7y ago · 157 comments
Anything that improves overall code quality of this language should be welcomed with open arms. Types, especially if they are backward compatible (as these are) will do so.
I'd be interested in what kind of automated or ide support will be available, but I suppose that won't be known until this is released later this year.
Other editors (VSCode for example) can hook into Psalm or Phans Language Server to get type information from these static analyzers.
Typing support in PHP Ides is usually excellent.
I don't know the state of the language server for newer php versions, but intellisense for vscode is too taxing on the CPU for relatively large projects (20k+ lines of code)
Already Chairs, Desk and Rent for every engineer is more expensive than 200 USD every year. Their salary is many hundred times 200 USD. I pay more for coffee and water that they can get for free every year.
Independent devs without income can get PHPStorm at much lower rates.
First, that $200 rate is for teams; independent devs can pay a significantly lower rate (the catch is, you have to pay it personally, not expense it or be reimbursed by an employer).
And even for teams, $200 a year is nothing. If you can afford to hire devs in a first world country (US/EU/Aus/etc), you can afford tools for them. NO employer you should possibly consider working for is going to quibble over the license fee; not when they're already paying 1000 times that in fully loaded costs in salary/rent/overheads/benefits/etc.
If you can't find $200 for a tool to help you do your job, you're not really a professional. (Again, in a 1st world country.)
- The yearly cost is purely for updates. Meaning you could just pay one time, get updates for a year and then continue using whatever version you have. - For small teams they offer a 50% discount (less than 10 people, in business less than 5 years) - If this is perhaps an open source project you can get it for free.
I got sucked into the Jetbrains family because of just how good their PhpStorm is. Especially in regards to refactoring and debugging tools.
I only wish their Javascript tools were anywhere near as good as their Php tools.
Don't they fall under the cheaper individual license?
Psalm supports all the same checks on typed properties that it already does for properties with docblock-defined types, and will make sure all your properties are properly initialised in a class's constructor.
Particularly for open source software it's hard to just say "from next version well use 7.4 min" so anything that can be added and fallback gracefully on older versions is handy.
Similar to browser docs what you're often interetested in is the latest things and how far back you can use them, and where they will degrade gracefully further back.
Those mostly assume you have old code that you want to make work on new versions, not that you want to write new code that takes advantage of as much new stuff as possible while still working reasonably well on old versions.
It's understandable that they would prefer that everyone just upgrade faster, so it's probably not something they want to encourage (both in documentation and in making features degrade gracefully if that requires any tradeoffs.).
This is a minor release and PHP tends to be almost completely BC even between major release versions so it's unlikely you'll face any issues.
If you want to start writing code that uses these features in an existing library then you can branch a new version that depends on PHP 7.4, and define that as the requirement in composer. That way users with older PHP versions will pull older versions of your library.
I find it hard to trust a language that changes so much between minor versions. If I am wrong please tell me where so I can stop my grief at work and finally upgrade the PHP version we use.
Is that true?
There are a few edge cases out there of course. Maybe your code base has basically been in stasis since the early days of PHP 5.x, may not even be using composer or namespaces, relies on tons of abandoned or horribly outdated dependencies, doesn't have good test coverage, etc., and then suddenly someone gets a bee in their bonnet about PHP 7.x and wants to upgrade. That's often not going to work out very well, but that's not really a PHP 7.x (or even a PHP thing). :) If you've been maintaining the code, have tests, no ancient vulnerable libraries, using current framework versions, etc, the process is going to be easy. If you're trying a big bang upgrade straight from the medieval period, not so much!
So maybe upgrading PHP itself might not be that bad, something I can't tell you for sure, but in my experience nobody uses vanilla PHP, they all wrap it in frameworks, and those must all be carefully upgraded too.
The past years we've seen several static analysers become more and more popular in the PHP community. Tools like phpstan (https://github.com/phpstan/phpstan) and psalm by Vimeo (https://github.com/vimeo/psalm).
Allowing this syntax is a great way to write cleaner code that can be statically analysed, which is far more superior than the runtime type checker.
Using GraphQL in the PHP backend und frontend TS. The schema is exported to TS and thus everything is fully types. A blessing.
All in all in 20 years we'll all be writing ml ?
[0] technological waves diffuse differently across cultures
The reason is that i want to know "on site" what exactly is going on when reading and debugging code, especially outside a "smart" IDE, like when diffing changes via the command line, a web-based VCS frontend or some code review tool - especially since these tend to not show the entire code, meaning i need to switch back and forth. But even with an IDE that can show me the inferred types, it is almost always (while i say "almost always" i really mean "always" since i haven't seen any other, but there might be some IDE i haven't tried) done through some popup under the mouse cursor, meaning it is transient and slow.
For similar reasons i dislike features like operator overloading, placing non-self-initializing logic in constructors (and the equivalent in destructors), properties for anything that involves complex calculations (unless the language uses special syntax for properties that make them stand out when used) especially for getters, i prefer to use names that hint to what the code does (e.g. i'd use something like "get_foo" only if it doesn't do any calculations and give you a precomputed or cached "foo", otherwise i'd use an "action" verb like "calc_foo" or "find_foo" or whatever to hint that there is some calculation going on and isn't just a dumb getter), i avoid exceptions as they create hidden exit points for functions (and if the IDE/editor allows, i use a more visible - like bold red - font for "return" or the language equivalent so that explicit exit points are obvious), etc.
Yeah, these make code more verbose and you need to type more, but IMO the negatives are grossly overwhined against and while the positives way too overlooked.
The tooling around has also improved so they can do it now.
I like the idea of language to support more type features because there is no one ultimate type of solution for everything and in every situation.
Moreover, these are all safe changes. There is less possibility for developers to abuse these features compared to what they can do with implicit conversion, for instance.
The trade-off between Java 7 and PHP 7.x is that Java has generics, but PHP's treatment of nullable variables is better/safer.
If everything goes well, yes. Everything is moving towards static typing with local inference.
If a static analyzer can detect these mistakes, why even add runtime checks for it? Think TypeScript/Javascript, after the compilation is done, most type checks are removed for performance reasons.
Or perhaps there is something I don't see...
Only when you then add them after the upgrade you should have tests for that or a static analyzer that tells you the changes you make are correct.
Still, static analysis is necessary when you upgrade dependencies and for your own code. For example for seeing whether adding a type to a property breaks code somewhere else. Discovering type errors at runtime is often too late.
Luckily there are good static analyzers for PHP.
I’m not 100% that it is a wonderful behavior, and a lot of people turn it off (which can be done on a per file declaration of strict) but for us it’s genuinely helped eliminate an entire class of error where a runaway type error would result in a really whacky value way down the line, even occasionally ending up with that stored in the database for some calculation.
The kind of thing we still end up with in JavaScript to this day.
https://dev.to/mindplay/php-typed-properties-think-twice-382...
Why in the world are people comparing PHP and Java?
They just ignored the fact that Java is compiled which makes all the layers of abstraction less of an issue whereas PHP is interpreted at runtime* hence why there are few high traffic websites on PHP that don't have at least four layers of caching.
* Yes, I know this is getting better with every version, opcache getting a lot of attention in the last few minor releases and all that, but there is only so much optimization you can do with dozens and dozens of layers of factories and services. My guess is that it does a LOT more for "simple" software like WordPress and less for, say, Symfony based projects.
I'm also seeing that with JavaScript and TypeScript.
TS feels very C# like.
Luckily there seem to be counter movements with languages like Reason, ClojureScript and PureScript.
C is not really moving, but C++ is also converging towards this Java++ language.
Java itself is finally moving.
C# is a bit ahead of the curve, because it can follow the trail blazed by F#.
PHP started moving towards Java back from when PHP5 was introduced.
Javascript is moving towards Typescript which is following C#'s lead.
Python is kind of the only non-Algol mainstream hold out.
Did I miss any of the big enterprise languages? :-?
Probably because they were created by the same person!
Here we get a pretty good deal, I guess time matters.
Usually, statically typed languages is not expressive. There are a lot of abstractions are hard to express in statically typed languages. Even in Haskell. With optional typing, you can express usual ideas with types, and cheat with 'any' when not applicable.
Another thing is the language itself can possibly execute without compile or even type check. Making developing feedback cycle much smoother when running tests or just save and refresh.
If the "Any" type is just a top type but you have to downcast it to use it, like in Kotlin, then it's not considered optional typing, since you still have to talk about types ;).
This adds up to essentially making everything nullable, regardless of if the type is defined as nullable, and adding an entire class of runtime errors that could have been avoided.
A new state was not necessary.
This is going to result in a new series of run-time-only bugs that PHP is famous for. I love the language but it's 2019 and we should be past this. Also, where are the generics?
Urgh. It baffles me that 10 years after their introduction, anonymous functions (closures) remain useless when assigned to instance properties.
class Foo {
public $var;
}
$foo = new Foo();
$foo->bar = function() { echo "Hello World\n"; };
$foo->bar();
Uncaught Error: Call to undefined method Foo::bar()
Callables work perfectly fine when assigned to standalone variables or array members: e.g. $foo[0](); But they become nonfunctional when assigned to instance properties. I understand that using closures as instance properties would make them hard to distinguish from methods, but blurring that distinction is just so damn useful in JavaScript! class Foo {
public $var;
}
$foo = new Foo();
$foo->bar = function() { echo "Hello World\n"; };
$foo->bar->__invoke();
Not pretty but that's how I've always done it.Example:
$c = new stdClass;
$c->test = function() { return 'hello world'; };
echo ($c->test)();<?php trait MetaTrait {
private $methods = array();
public function addMethod($methodName, $methodCallable)
{
if (!is_callable($methodCallable)) {
throw new InvalidArgumentException('Second param must be callable');
}
$this->methods[$methodName] = Closure::bind($methodCallable, $this, get_class());
}
public function __call($methodName, array $args)
{
if (isset($this->methods[$methodName])) {
return call_user_func_array($this->methods[$methodName], $args);
}
throw RunTimeException('There is no method with the given name to call');
}
}
?>test.php <?php require 'MetaTrait.php';
class HackThursday { use MetaTrait;
private $dayOfWeek = 'Thursday';
}$test = new HackThursday(); $test->addMethod('when', function () { return $this->dayOfWeek; });
echo $test->when();
($foo->bar)();
Methods and properties are (perhaps regrettably) separate namespaces in PHP.Prototyping stuff without bothering with types and adding them when your code matures to root out some bugs and make it more futureproof seems to me like the best of both worlds.
Your chickens will eventually come home to roost, but the short-term productivity gains are quite pleasant.
Seriously I have noticed that static vs dynammic preference is quite sustainable thing for people. Even if you are have used lot of both most people prefer the one they are used when they 'got it' during their programming learning path.
I don't really understand that CSV example what's the problem but one concrete challenge I have noticed with static types is when you want do mutation which effectively change the type. Instead of setting just new property you have to potentially do dummy copy (including type) or some additional hashmap by id for this additional data.
(btw I am not your down voter)