Creating type-safe configs in Laravel
This is a great way of handling config values.
Read more [cosmastech.com]
This is a great way of handling config values.
Read more [cosmastech.com]
One of the most sought-after features for PHP is Generics: The ability to have a type that takes another type as a parameter. It's a feature found in most compiled languages by now, but implementing generics in an interpreted language like PHP, where all the type checking would have to be done at runtime, has always proven Really Really Hard(tm), Really Really Slow(tm), or both.
Read more [thephp.foundation]
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"Always fresh, useful tips and articles. Carefully selected community content. My favorite newsletter, which I look forward to every time."
In this post, I'll share how I'm using PHPStan's type aliases feature to make validation rule typehints more readable and maintainable in our Laravel application.
Read more [ohdear.app]
Utility types are types that modify other types. You can think of them as functions, but they operate on types instead of values.
Read more [piccalil.li]
Here's how you can use our Laravel Data package.
Read more [matthiasweiss.at]
Generics have been on the list of wanted features for a long time by numerous PHP developers. This article explores the different approaches, and what their current state is.
Read more [thephp.foundation]
– tighten.com - submitted by Jamison Valenta
Let's explore Zod: a library that can help us validate data not only on the client but also on the server, and even generate TypeScript types to maintain end-to-end type safety and consistency.
Read more [tighten.com]
– jamie-peters.co.uk - submitted by Jamie Peters
Have you ever been using the Laravel Request class and found yourself creating single use variables just for type casting? What if I told you there's a better way...
Read more [jamie-peters.co.uk]
A nice idea by Roman
Read more [pronskiy.com]
– ryangjchandler.co.uk - submitted by Ryan Chandler
Let's take a look at how we can create a custom Option type to handle optional values in PHP.
Read more [ryangjchandler.co.uk]
Generics are available since PHPStan 0.12. This posts explain what they’re all about.
Read more [phpstan.org]
Thanks to the improved type variance in PHP 7.4, the `self` return type hint is now safe to use in a class that you expect to be overridden (happens a lot in the packages we create)
— Freek Van der Herten (@freekmurze) December 3, 2019
Demo: https://t.co/7vRTaOwRhD
RFC: https://t.co/1RpnFIjlKC#php pic.twitter.com/SfAaxRDt4m
Read more [twitter.com]
When the void typehint was introduced in PHP 7.1. There was some debate about it. Some people wondered if it is beneficial to type nothing? I was one of them. Meanwhile, I changed my opinion on it. In this short post, I'd like to give you a small example where I think void shines.
Frederick Vanbrabant, developer at madewithlove and co-organiser of PHP Antwerp, explains on his blog what generics are.
So everyone is talking about this hip “new” kid on the block for PHP: Generics. The rfc is on the table and a lot of people are getting all excited about it, but you don’t fully see the excitement? Let’s explore what it’s all about!
http://frederickvanbrabant.com/2017/05/31/generics-in-php.html
On the stitcher.io blog a new post appeared that explains the benefits of generics with a practical example. Generics aren't supported in PHP, but there is an RFC.
In today's blog post we'll explore some common problems with arrays in PHP. All the problems and issues listed could be solved with a pending RFC which adds generics to PHP. We won't explore in too much detail what generics are. But at the end of this read you should have a good idea as to why they are useful, and why we really want them in PHP. So without further ado, lets dive into the subject.
https://www.stitcher.io/blog/php-generics-and-why-we-need-them
You might thing that PHP is not able to automatically perform a type check on items in an array. But using variadic constructor this is possible. Bert Ramakers wrote a blogpost with some good examples on how to do this.
One of the language features announced back in PHP 5.6 was the addition of the “…” token to denote that a function or method accepts a variable length of arguments.Something I rarely see mentioned is that it’s possible to combine this feature with type hints to essentially create typed arrays.
PHP 7.1 is nearing completion. A few days ago Release Candidate 1 was released. One of the areas that got some love from the developers is the type system. Pascal Martin wrote a blogpost about those changes.
One of the most important changes PHP 7.0 brought us last year was about typing, with the introduction of scalar type-declarations for functions/methods parameters and their return value. PHP 7.1 adds to those type-declarations, with several points that were missing in the previous version of the language.
https://blog.pascal-martin.fr/post/php71-en-types.html
That new iterable pseudo-type will sure come in handy.
If you want to know what's changed regarding error handling, Pascal has got that covered as well.
In a new post on his blog Thijs Feryn explains type errors and strict types, both introduced in PHP 7.
PHP 7 has a concept called Type Errors. These errors are thrown on a type mismatch when interacting with functions. They can be caught just like exceptions. I created a video that explains the situation. This blog post goes into more detail and has some code examples.
Mathias Verraes illustrates with practical examples how PHP's type system can be used when designing a system.
Below is an attempt at illustrating a design/redesign process I went through at a client, who’s started refactoring the core systems their business depends on. Design is the part of software development that is the most messy, the hardest to fit into rules or well-defined processes. In fact, while writing this post, I tweeted:http://verraes.net/2016/02/type-safety-and-money/None of the solutions offered below should be taken as truth. I may have already changed my mind on some of them by the time you read them.