PHP Modern app development
PHP? Why I would use this pitiful language?
PHP has a bad reputation among many programmers — some of them because of the language's past mistakes, lack of standardization, security issues, and code styling. That's why new programmers don't see PHP as an valid option for developing their projects.
For your information, PHP 7 has overcome so many past issues and improved speed. Now PHP is insanely fast and has type hinting for methods and return types, making the language more consistent.
It has a bunch of helpers methods to do more while writing less. You can see a list of the improvements here.
If you are a current PHP user and you have a version lower than 7, please migrate. PHP 5 lost support this past year, and migrating from one to another has a low rate of issues.
That's a lie, nobody uses PHP anymore...
Some big players, among others, are using PHP or some components of the language:
- National Geographic.
Name some advantages of working with PHP
- Nice documentation and support, the community is very big and kind, and they love to help.
- It is reliable, stable, and mature. The PHP staff isn't throwing new functionalities that break some old ones and they have a very professional roadmap of releases.
- Has a very low learning curve.
- Can be used in projects of all the scales: little, medium, and big projects could be written in PHP.
OK, got it! I want to write my new cool application in PHP. What can I do to start?
Please, oh god please, don't use WordPress for everything.
WordPress is a platform to make blogs. Any other thing out of this scope has a better tool. A lot of PHP's bad reputation is thanks to WordPress misuse and some other bad practices of programming in the core of WordPress. Please use the right tool for the right job.
Avoid using core PHP to build your project from scratch, unless you have a very good reason to do it. Use a framework instead — they have a set of common code to improve security, make the maintenance of your application easier, and let you focus on the important thing, your business logic.
Which framework should I use?
In my personal opinion, PHP frameworks have two big players right now, Laravel and Symfony. I use both for long-term projects. Both are great to make the things fast and right with a standard way to do things for you alone or for all of your team.
Let's take a look at their main advantages and disadvantages.
Laraval is a framework made by Taylor Otwell and defined as a Framework For Web Artisans. Laravel has been around since 2011 and has gained a lot of popularity since then. It's currently the most used PHP Framework.
- Easy learning curve. If you are a traditional PHP developer, you will find the transition quite smooth.
- Has a nice set of out of the box characteristics, such as user management. Laravel comes with user registration, login, notifications, and an authentication system that allows you to move faster building your app.
- Documentation is very good and oriented to newcomers, I think that maybe it's the best documentation of frameworks that I've ever read.
- Collections. Personally, I think that is the best thing in Laravel. The collections helper allows you to parse arrays of data in an elegant way, like lambdas for Java, they can be extended as well to write your own collection functions.
- Their templating system blade is terrible. It reminds me of the old way to write PHP code. This can be solved using Twig as a template engine.
- Eloquent ORM can be improved too. It's based on Active record pattern, and still too much SQL language for my personal preference. This can be solved using Doctrine ORM instead.
- Unit testing of certain components can be a pain. It has some helpers but really isn't enough, even the creator of the framework had some awkward moments on Twitter trying to explain how to test some components.
- The (ab)use of the facades. You have to install a special package in your project to help the code recognition of your IDE — PHP Storm in my case.
Symfony is a framework made by Fabien Potencier. It's defined as a set of reusable components and a PHP framework for web apps. It has been around since 2005, is number two in popularity when it comes to PHP frameworks, and many other frameworks including Laravel use Symfony components.
Symfony, 2 stages
Symfony 3 and lower's philosophy was to make a full MVC framework to build web apps, without any external integration, that could possibly reduce the time for developing core frameworks
functionalities or improvements and lower the performance of your project by adding more lines of code to the framework base. They let the community make the integrations and functionalities, like user management, for example.
Symfony 4 comes with a new philosophy. It began as a micro framework and grows with the needs of your project, which allows a change of the paradigm. Now the core Symfony development team is developing integrations and functionalities (like the Laravel team) to speed up your development even more. This is a good change of direction.
The pros and cons are based on the Symfony 4.
- Keep an eye on best practices in coding style, you can build fast without sacrificing quality.
- Use Doctrine as ORM, is based on Repository, has great toolset, and many functionalities that make it the top PHP ORM, in my opinion.
- Twig, is a powerful templating system, is meant to be simple and can be easy to understand, even if you know nothing about PHP.
- The debug mode is a great way to see what is going on when you have an error.
- Symfony flex now automatizes all of the configuration of the bundles (Symfony extension packages). You only have to change the config values.
- Symfony components are used by very big players in the tech arena.
- The learning curve is a little higher than other frameworks.
- Twig has a downside — it's a little slower than other templating systems.
- Longer to launch. Using good design patterns is a Symfony practice, which makes it more testable and easier to read, but takes more time to get to production.
PHP isn't dead. It's a great language that allows you to deliver your product in a fast and reliable way, if you're going to use it, please use the right tool for the right job and choose a framework that meets your needs.
I hope that this article helps!