PHP 7/8 Syntax in Laravel: Do You Use it?



For those who learned PHP before PHP 7, there are quite a lot of syntax improvements since then, to make the code more readable. Did you adopt them?

– – – – –
Support the channel by checking out our products:
– Try our Laravel QuickAdminPanel: https://bit.ly/quickadminpanel
– Enroll in my Laravel courses: https://laraveldaily.teachable.com
– Purchase my Livewire Kit: https://livewirekit.com
– Subscribe to my weekly newsletter: http://bit.ly/laravel-newsletter

39 thoughts on “PHP 7/8 Syntax in Laravel: Do You Use it?

  1. How roes it work when defining what the method is returning, if it includes a Try/Catch for example? If it throws an exception, is that an issue vs returning a collection?

  2. Hell. Please don't recommend to pass any data arguments via constructor. That makes service stateful and disallow to reuse of them. Data should be passed via params of methods . Use constructor only for dependencies and configuration

  3. Is (?int $projectID) the same as (int $projectID = null)?
    And, how do you know when something should be type strict or not?

  4. Hi guyz..
    I am creating an application for my organization and i am thinking how to properly represent a User that own a Department (Admin User for that department) and also that same User as a Staff of that Department

    I have models like User, Department, Assignment

    For the ownership (Admin User)
    User -> hasMany(Departments, owner_id)
    Department -> belongsTo(User)

    For the assignment, I used a table (assignments) that has user_id, department_id also i was thinking of using department_id on user_data table..

    Any suggestions to an older video that talked about this?

  5. More than correct/incorrect I think it's responsible to document our code and using this php 7-8 features for type hint is super useful and makes it easier to debug hidden issues.

  6. The only thing about Union Types that I enoyed, is the ability to type-hint all kind of array variables, since we can have, Collections, any Arrayable interface and the native array type. A simple helper or method that can iterate something to call a method and perform operations with the data can be very overwhelmed by the many different "array" types.

  7. Just one tip from me… When you need a constructor you dont need to type "pubf"… Just type "__const" and you can have really quick a constructor.

    On union type I have to disagree with you. The int|float is a great one when you have a function that is returning a price. The number 3 ist an int but 3.14 is a float.

    That is perfectly normal when you dont have your prices as cent in the DB.

    Other than that, your videos are a daily for me and I enjoy them all.

    Thank you

  8. Thanks for you videos! They are always super clear and easy to understand!
    I would love to see a video about database transactions and concurrent requests.
    It might be not related to Laravel specifically, but I lately had to explain some other developers why they had duplicate entries in database in case of concurrent requests, even if they were using transactions.
    Talking about transaction isolation level might be too much, but explaining why we might have duplicate entries, and how to handle this situation might help a lot of people 🙂

  9. Dear @Laravel Daily! At 3:45 you are right, but not exactly. Eloquent's get() method return IlluminateDatabaseEloquentCollection, which is instance of IlluminateSupportCollection, but it has a few additional for the contained Models. E.g: getKeys().

    Btw, PHP allows you to use the parent class as return typehint. So if class B extends class A, and your method return class B, you can you A as return typehint, but IDE will not see the methods of class B in code completion.

  10. For variable name projectId you type hinted to int in function but it only work if you kept the id as route parameter binding if in future route parameter binding is changed from id to other field i.e. slug etc. Then we need to change the code base. If we type hinted to Project model than it work irrespective of which value is used for route model binding.

  11. Hello! doesn't one actually then also set something like "public function __construct(int projectId) with the contstructor or does one no longer need this because one uses "typed properties"?

  12. I always type hint everything. It helps my IDE too because it knows the type of variable and i can use auto complete features. You can also eliminate the annotations so u get even less lines per file

  13. Hello, I stuck with another problem, I have a buy product button, wich on click checks balance, create order and put to product is_bought = True. But i found some security issue, if i press this button quickly (several times in one second) it pass every time balance validation, and buy this product each time(but one product can be bought one time))(controler took time for work, becouse it also connects others api), looks like the first request didn't finished, but script already works with second request, It can bring a lot of problems. I read about pessimistic lock (->lockForUpdate()), but in Lara docs not to much about it and nothing understend. Plz make video how to buy product coretcly, without security issues.

  14. I've started using this syntax since last year. It's a great way to comment the code for future readings like you said. The projects I'm working on are still on PHP 7.4, so I haven't had the chance to explore PHP 8 syntax yet.

  15. Great video, definitely something I stick by. I also found that another great reason to be strict about return types: By their nature they will break the code execution, allowing me to find bugs I had otherwise overlooked when I setup testing. Small things like the wrong json resource being returned as a default value, to an empty string instead of null.

Leave a Reply

Your email address will not be published. Required fields are marked *