4 ms·
It was long ago I worked with PHP, before PHP 5. Is typing enforced with PHP these days? Or is it more like type hints, as in Python?
by sedeki 7y ago
It was long ago I worked with PHP, before PHP 5. Is typing enforced with PHP these days? Or is it more like type hints, as in Python?
- chx 7y agoObjects are enforced and a simple declaration can make PHP enforce the scalars as well. https://3v4l.org/6UU2v https://3v4l.org/6UU2v https://3v4l.org/q1PAq https://3v4l.org/q1PAq In PHP 7.4 due in a few weeks covariant return types / contravariant arguments will arrive. https://stitcher.io/blog/new-in-php-74#improved-type-variance-rfc https://stitcher.io/blog/new-in-php-74#improved-type-varianc...
- sedeki 7y agoIt seems as if declare(strict_types) works for scalars as well (first example)? However, if I remove that declare from your latter example, only one output is shown. Edit: Nevermind, I get it.
- pilif 7y agoFor classes it's enforced since PHP 5. For scalars you get to chose between enforcement or type coercion for callers on a per-file basis. However, as with other dynamically typed and interpreted languages, all of this is happening at run-time, so one of the biggest benefits of strong typing (type checking at compile time) doesn't apply.
- weberc2 7y agoYou can statically type check a dynamically typed, interpreted language. Python via Mypy and Typescript being two examples.
- j_jochem 7y agoUnfortunately, PHP doesn't ship a tool for this (and it’s hard due to the way autoloading works). So PHP's typing is slightly less useful than e.g. TypeScript.
- beberlei 7y agoThere are phan, php-stan or psalm tools from third parties which add this functionality to PHP. All three of them are very good and actively developed.
- Master_Odin 7y agophpstan, phan, and psalm all exist and can provide these sorts of static analysis checks.
- j_jochem 7y agoThey do static analyses that go beyond type checking. I have not tried phan and psalm, but the last time I used phpstan its type analysis threw false positives because it was using incorrect assumptions. So my impression is that phpstan lacks the rigor of a true compiler‘s type checker phase. In my opinion, the PHP distribution shipping an official type checker which does nothing other than verify type correctness based on the information in the code would be much more useful. Kind of like `php -l`.
- normaljoe 7y agoTooling is not the issue here, nor is autoloading. ISO C and C++ doesn't provide tooling, nor should any language. Tooling is better handled by third parties and implementers. Language should be focused on application and execution of the language. Unfortunately for PHP the language and implementation are tightly coupled as is Java and Swift. A well constructed modern PHP project using composer has the tooling needed to statically validate. Personally I use PHPStorm but I also use IntelliJ for Java and Android. There are others out there as well. Autoloading used correctly is no more than Java using a package line or namespace. I find no problem with TypeScript and others. I however have a 10+ million line code base of PHP that predates TypeScript and others and needs to get a viable transition path. This gets things closer with real type errors at runtime. That is way better than my mainframe COBOL counterparts who have no path forward at the level of modern code.
- cutler 7y ago
- beberlei 7y agoSorry to interrupt here, but this is wrong. it is not enforced to use types for anything (arguments, return values or properties) if you don't want to. Using it is completely optional. Edit: Ah I believe there is a misunerstanding between posters here. The usage of types is not enforced, but when you use them their correctness is enforced at runtime.