6 Things I Learned From Modern PHP: New Features and Good Practices
I recently read Modern PHP: New Features and Good Practices and I thought I would pass along several things that I learned while reading the book.
Overview
Modern PHP: New Features and Good Practices is “modern” up to around PHP 5.6 so it doesn’t cover any of the new 7.0 features (it actually recommends Hack near the end). It does a good job of pushing the idea that you should be using composer to load in libraries that do what you need instead of reinventing the wheel which I love. I would recommend anyone who has a basic PHP understanding as it covers a lot of helpful topics that may not be covered in other sources.
1. Traits
Traits are an interesting feature in PHP. They allow you to define a structure then reuse the methods and properties of the trait in multiple classes and have multiple traits in the same class. This is a work around to the fact that PHP doesn’t support multiple inheritance but it seems to work REALLY well.
Output:
2. Generators
Generators allow you create dynamic iterators without needing to create a class that supports the iterator interface. I find this to be a interesting concept for abstracting away large data sets but I haven’t been able to wrap my head around a really good use case. The power behind this feature comes from the yield
keyword which returns the value but causes execution of the yield
ed function to restart when the next value is needed.
Output:
3. Built in Server Has Drawbacks
PHP has a built in server so you can test your sites.
I’ve never used it because I prefer the VM experience but it turns out there are actually problems that would prevent people from using it:
- Only one request at a time
- Limited mime types
- Limited URL rewriting
Just more reason to us VMs with a full Apache install.
4. https://github.com/ziadoz/awesome-php
https://github.com/ziadoz/awesome-php is a list of good PHP libraries and resources. I wasn’t even aware this existed until I read about it in the book so it will be my goto resource for libraries.
5. https://github.com/filp/whoops
PHP errors for cool kids
https://github.com/filp/whoops
This looks like a cool project to clean up the errors that PHP displays and I’ll be integrating it into my development environment.
6. XHProf
XHProf is a profiler developed by Facebook like Xdebug but it can run on production environments without a huge performance hit.
Conclusion
Overall, I really enjoyed reading Modern PHP: New Features and Good Practices. I think it would be a good book to read once but I’m not sure I’ll ever refer back to it.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows