5 ms·
I've asked this question a couple of times on stackoverflow, but it get's removed, even though it is a legitimate question to me: I have found that working in
by neals 4y ago
I've asked this question a couple of times on stackoverflow, but it get's removed, even though it is a legitimate question to me:
I have found that working in a team with php has made it, sometimes, difficult to catch bugs that a typed language would prevent at compile time.
How do larger teams work with php and frameworks like Laravel and prevent errors that are related to type-checking?
- thinkindie 4y agoyou use some static analysis tools like phpstan and psalm - I personally use phpstan and it helps a lot. Obviously you must use all the type hinting where possible and use annotations where the native ours are limited
- lnxg33k1 4y agoIt depends on the team workflow, php can be very relaxed or very strict about types, to check correctness when you pick strict modes you have tools like PhpStan, but you can also verify correctness with PhpUnit
- fndex 4y agoPHP has static types.
- alexriddle 4y agoPHPstan (or Psalm) catch any type safety issues for me. I use types extensively in my PHP code.
- sroussey 4y agoUse the type checking features of the language. They may be optional, but they exist.
- amerkhalid 4y agoI have worked on a large PHP monolith with 100+ devs. We rarely had any major bugs make it to production. I think unit tests really helped there. Of course, having QA department helped a lot too but unit tests are first line of defense. And we also used all other best practices such as PSR code formatting standards, linters, code reviews, etc.
- unculture 4y agoLots of people, particularly Laravel people, are doing static analysis with tools like PHPStan or Psalm. Over time there have been more and more type-oriented features introduced to the language too. Obviously you can't catch everything, but it's a dynamic language so that's the trade off you've signed up for.
- lostfocus 4y agoBasically what everybody else said: tests and a static code analyzer.
- simion314 4y agoI used PHPDoc to document stuff, now there is native support to declare types. Then my IDE (Intelij) will highlight type related issues as errors or warnings. It will also show you lot of other errors like using the wrong parameter count, the wrong parameter types etc. With PHPDoc you can also document "magic" stuff like objects that are auto-generated from DB tables, so you get error checks and autocompletion on column names. But I work on a small team. We use PHPUnit for unit testing, but this is not related to types.
- user3939382 4y agoI'd like to combine and expand on a few answers you got. Firstly: > bugs that a typed language would prevent This sounds like you think PHP isn't typed. It is, and with 8.1 (and even more so 8.2) the type system isn't exactly Haskell (ha) but gets much stronger than it historically has been for PHP. You enable a feature called strict_types which enforces these rules. Then you have a layer of even more powerful type checking and analysis which, though not enforced at the runtime level, is picked up and validated against by your IDE's real-time (or async external) static analysis tools. This is partially in phpDoc and there are various ways to do it. Then, besides static analysis with popular tools like PHPStan or Psalm, there are nice inspection tools like EAInspections, Sonar, etc that check for hundreds of possible errors, code smells, style issues, etc. Large, professional PHP codebases are not a big wild west of implicit coerced dynamic types. For my own projects, I consider any avoidable reliance on PHP's dynamic typing a bug, i.e. casting null to bool in conditionals.
- jw1224 4y agoI wrote awful untyped PHP 5.6 code for many years, using CodeIgniter and a very basic editor. Fully embracing PHPStorm, strict_types and EAInspections completely changed me as a developer. I hated them at first, just getting in the way and annoying me… But now several years later and I fully credit them for revitalising my career and giving me a newfound love for coding.
- Ayesh 4y agoYou just keep adding types, it gets better by time. When you do add types, go with strict types (strict_types=1). Type errors pop early when there are many type enforcements. When you find parts that you can't easily type (open file handlers for example), you can create wrappers that encapsulate the handlers. Also, use phpstan/Phan or other static analyzers like others mentioned.
- efficax 4y agostatic analysis does a lot, but yes, dynamically typed languages mean runtime bugs that a compiler would catch. this is true of php, but also javascript, ruby, python, most lisps, just oodles of languages that people build huge systems with. i'd prefer evryone embraced compile time type checked languages, but here we are.
- tyingq 4y agoLots of answers on using the type support that is there, but some use of forced casting is also common, as is using === when appropriate, avoiding "if ($bool)" in favor of "if ($bool === true)" for boolean comparisons, using linters, static analyzers, and so on.
- jw1224 4y ago> using === when appropriate It’s always appropriate. A good IDE should highlight == as a warning. My code quality and programming abilities got significantly better since forcing strict types and comparisons on myself. I can’t imagine writing PHP any other way now.
- rossriley 4y agoThere are a few instances where using == is desirable, for instance it allows you to compare two instances of an object and returns true if the properties are the same. In this case === will always return false since it checks if the two instances are the same object.
- darkhorn 4y agoUse PHP 8.1 with typed properties and parameters, PHPDoc, PHP Storm with xDebug, unit tests, code review by peers, Composer, and declare(strict_types = 1);
- TheCapeGreek 4y agoOthers have explained how PHP can be statically typed. Here's how to enforce it: Set up a linting rule that every project PHP file (with some exceptions, like templating files) needs to have the `declare(strict_types=1);` directive. Then you can also have PHPCS fix it or just generally have linting checks on PRs. For large legacy apps you can still add exceptions.
- jhoelzel 4y agothe same way you check every other code that will join the system: pipelines. I know you mean: "when i hit compile i dont see the errors directly" but that can be achieved by activated the strict type setting and your screen will turn red too =) I really think that, from all the frameworks i have used, symfony actually has the most decent debugging system. it comes with tracing logging and live debugging. so pretty much everything you would get out of any other language. With xdebug and editors like phpstorm you are also able to pretty much mimic working with PHP as you would with C#. I believe there are working plugins for Visual Studio too. Just as an excourse, I have also worked with projects that compiled php to other languages too on the fly.
- mcluck 4y agoRe: your Stack Overflow woes Stack Overflow makes a lot more sense when you think of it as Wikipedia run by mathematicians rather than a forum. If your question is ambiguous or open for discussion, it's probably not a good fit for Stack Overflow. I don't spend much time on there anymore but now you know
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]