Strict Variable Types in Laravel: Practical Examples

Sdílet
Vložit
  • čas přidán 10. 06. 2024
  • Do you define variable types in Laravel/PHP? And, do you declare them as strict? Let's look at practical examples of when those are useful.
    Links mentioned in the video:
    - Ash Allen's PR to declare strict types: github.com/ash-jc-allen/find-...
    - My tweet asking the audience: x.com/PovilasKorop/status/179...
    - My course lesson: "Larastan: Catch Bugs with Static Analysis. Example 2: Missing Types" laraveldaily.com/lesson/laras...
    - My video. Parameter Types in Laravel 10: Why It's So Important • Parameter Types in Lar...
    - My video. PEST: Auto-Detect env(), dd() and Other "Leftovers" • PEST: Auto-Detect env(...
    - Reddit: "Why does noone use `declare(strict_types = 1);` in Laravel project." / why_does_noone_use_dec...
    - - - - -
    Support the channel by checking out my products:
    - My Laravel courses: laraveldaily.com/courses
    - Filament examples: filamentexamples.com
    - Livewire Kit Components: livewirekit.com
    - - - - -
    Other places to follow:
    - My weekly Laravel newsletter: us11.campaign-archive.com/hom...
    - My personal Twitter: / povilaskorop
  • Jak na to + styl

Komentáře • 25

  • @pvtw
    @pvtw Před měsícem +11

    What I do is using laravel pint to add strict types to all my files. Just add "declare_strict_types": true, to the rules object in pint.json.

  • @theceilidhboy
    @theceilidhboy Před měsícem +6

    100% should use type hints with strict types on 👍

  • @learn-web-dev
    @learn-web-dev Před měsícem +1

    I have been using it for a lot of years now. It prevents auto conversions of data. Being as strict as possible is the way to go, even if the average PHP dev won't agree with me :) When you create/maintain corporate projects for some years, you understand that writing code should follow standards (PSR-12 enforces strict types), should have clean architecture etc. And after you understand it, you will write code like that even for a 5 minute presentation. Who follows standards, will follow it all the time. PHP is becoming stricter and stricter and that is very good. Incorrect ways of writing code like: explode('separator', $variable) will no longer work if $variable is not 100% a string and that is very good. You should always know the exact type of your data, and that type should be one, if possible. Code like: "if ($variable)" when $variable is not 100% a boolean should be avoided. When you will understand why, you will be a better programmer :)

  • @user-ir6lv2nq8i
    @user-ir6lv2nq8i Před měsícem +8

    old phper: Type safety? I solve everything with mixed! 😎👍

  • @IOSALive
    @IOSALive Před měsícem +2

    Laravel Daily, My guy, your channel is amazing. Let's collab!

  • @ShaquilleDaniel1913
    @ShaquilleDaniel1913 Před měsícem

    Regarding declaring this globally, can it be set in the "\app\Providers\" boot function?

  • @stojankukrika7242
    @stojankukrika7242 Před měsícem

    @LaravelDaily some side question. Which app you are using to mix screen, tab in browser, phpstorm, and your small camera video?

  • @SXsoft99
    @SXsoft99 Před měsícem +2

    I will be honest I thought this was enforced by default with php types. my bad for not reading the RFCs like i should have.
    Now to be honest i think that using type enforcement, adding phpstan, adding even more code analysis depends on how concise your code needs to be. If you are making a package you want to enforce types like there is no tomorrow, if you are just making a presentation website or a short api configuration panel you might not need to enforce strictness.

  • @ripplesr5655
    @ripplesr5655 Před měsícem

    Jr question. Should we declare the strict type everywhere we are using types or just in the controllers?

  • @chlouis-girardot
    @chlouis-girardot Před měsícem +2

    It's a php type juggling issue

  • @nimmneun
    @nimmneun Před měsícem +1

    Although it can be a bit of a hassle to have to explicitly typecast more often, it really does help catch bugs that went unnoticed before, if you declare strict types.
    I always use strict types nowadays unless I have a case where I specifically want type coercion.

  • @Van4kk
    @Van4kk Před měsícem +1

    But is there a way to declare stric types globally in the project??
    I mean, if you have a project with thousands of classes, it will take some time to declare strict type for a classes ))

    • @Van4kk
      @Van4kk Před měsícem +1

      I can set the php cs fixer to do this for me, but it may broke the code because it will run only when pipelines are running

    • @LaravelDaily
      @LaravelDaily  Před měsícem

      Not sure if it's possible globally, haven't seen it done that way.

    • @user-e6strzInp1n
      @user-e6strzInp1n Před měsícem

      Reminds me the same in JavaScript some time ago. It must be enabled globally, someday, with ability to disable locally for some (legacy) folder. But anyway, this is good example why php is shamed in programming community.

    • @ercog7921
      @ercog7921 Před měsícem +2

      ​@@user-e6strzInp1n This community is 5 years behind real life 😅 You can find many videos why php is not as bad as before

    • @ratchet27
      @ratchet27 Před měsícem +1

      with Rector yes, easily, its one of the rules iirc