Engineering

PHP 8.0: How to Get Started & The Most Interesting New Features

PHP 8.0: How to Get Started & The Most Interesting New Features

PHP 8 is a major update to the PHP programming language, released on November 26, 2020. According to the official page, this most recent version contains several new features and optimizations. In this post, I will tell you about some of the most interesting new features and detail how to get started. But before that, let’s talk a little about why I love the PHP language.

What Makes the PHP Coding Language Fantastic?

PHP developers are used to dealing with jokes about being bad developers, but thankfully, this has changed recently. The truth is that being a good developer does not depend on the technologies, tools, or languages they use. Instead, being a good developer results from the qualities, skills, and experience acquired from doing their work properly.

From a business perspective, PHP has evolved significantly. Many companies are using the PHP language to create different applications. Since these companies are constantly upgrading their PHP versions, they can build new features and reach their business goals faster.

A good example of this is serverless PHP applications. I like to think that one of the significant advantages of using serverless is that we, as developers, can release code without thinking about servers. The cloud-based execution model is revolutionizing the industry, even though it’s not a new concept. Many companies are just starting to utilize serverless advantages like modularizing applications, scalability, reduced server costs, improved deployment process, etc. This paragraph alone can provide material for another blog post highlighting that PHP is not dead, contrary to what some people believe.

Nowadays, as PHP developers, we have the opportunity to work with cool stuff. Frameworks like Laravel and Symfony changed the way we set up new projects. CMSes like Drupal gave us the power to control our customization of content pages, and other contributions made by the PHP community have opened a world of opportunities.

And that is not all; today, we are enjoying the latest version of the language.

From my experience working with PHP for years, if I had to name an essential feature of the language, I would say it is the community. The PHP community is one of the warmest and most active communities nowadays, from the creator Rasmus Lerdorf to those who maintain the core of the language, people involved in creating and improving frameworks, the documentation, and the companies that use it and share their knowledge. It’s an amazing community, and I love being a part of it.

Now, let’s get into PHP 8. 🥳

Getting Started With PHP 8

If this is your first time working with PHP, lucky you! PHP 8 is an excellent version to start with. Some groundbreaking changes were introduced in this update, and I will describe some of them in this post. Keep reading if you’re interested in joining the PHP world.

If you have experience with another PHP version, you will need to make some changes in your code to get it running. You will have it easier if you’re updating from version 7 because most of the changes were deprecated before version 7.

Recommendations for the Transition to PHP8

  • Try to get familiar with the documentation
  • Do not resist the changes: as time passes, it gets more difficult
  • Consider the effort: This will depend on:
    • Your current version (as mentioned earlier)
    • The size of your application
    • The priorities of your business
  • Be aware of the advantages
  • If you are working on a big project:
    • Divide and conquer
    • Take baby steps
    • Make sure to finish what you start

The Most Interesting New Features

Constructor Property Promotion

Who does not like to see fewer lines of code? Well, with this RFC, a shorthand syntax was added. We can now combine the specification of the class properties at the constructor.

Before, your code looked like this:

Now it can look like this:

There are some constraints and specifications that you can review on the official documentation.

Union Types

There have been times as a PHP developer where I wanted to use a more “flexible” variable. Considering the dynamic nature of the language, it can be useful. A “union type” is a collection of multiple types that indicates one of those types can be used.

PHP 8 already supports: 

  • Type or null, using the special “?Type” syntax
  • Array or Traversable, using the special iterable type

In the above example, the input argument can be of type Foo or Bar, and the function returns either an int or a float.

There are some exclusions, especially with the void type. Please review the official documentation for more information.

Named Arguments

This RFC allows passing arguments to a function based on the parameter name instead of the parameter position. A benefit of this new feature is that you can now skip optional parameters, which allows you to keep clean and understandable code.

VS

You can read about the benefits, attributes, and more on the official documentation.

JIT Compiler

The JIT acronym means “just in time.” JIT is a compiler that promises significant performance improvements. On the web, you can find some documented posts with tests made by PHP developers comparing processes with and without JIT, and in some cases, they got a ten times performance increase using JIT.

With this compiler, there are some new opportunities for PHP developers that didn’t exist before, for example, machine learning, AI, etc. For me, this sounds promising. On the other hand, when thinking about web applications, you should know that there is no significant impact for you in using JIT right now.

Other Improvements That Caught My Attention

PHP 8 has added a few more warnings and changed others to exceptions, for example:

  • Warning to dereference null objects and arrays

When I try to access a key inside a null array, PHP 8 shows “Warning: Trying to access array offset on value of type null.”

In object references, it shows “Attempt to read property {foo} on null,” the same warning that appears if we try to access a property on a null object.

In PHP8, TypeError and ValueError were introduced. With these exceptions, types and value validations were enforced. The significant advantage of this enforcement is that we can now discover bugs early.

Further Thoughts

If you are thinking about upgrading your PHP version, do it securely; do not forget to visit the list of breaking changes in the upgrading document

Remember that the level of complexity will vary depending on which version of PHP you’re upgrading. If you’re upgrading from a version greater than version 7, it might be more straightforward. 

In this post, I also want to include a book recommendation, “RECTOR – The Power of Automated Refactoring,” written by two great members of the PHP community, Matthias Noback and Tomas Votruba. This book is helpful in the journey of refactoring a PHP codebase.

Conclusion

In this post, I only mentioned a few of the many features of the latest PHP update. If this post caught your attention and you want to choose your favorite features, visit the official documentation to get familiar with all of them.

In a couple of months, in November specifically, PHP 8.1 will be released, and more exciting updates will be coming. If you want to see a complete list of them, you can visit the RFC PHP 8.1 list on the official documentation.

Keep in mind that the tech industry is constantly improving. As software engineers, we must be aware of the things that can make our work easier. Updates are usually one of them. I hope this post will be helpful for those who are interested in starting to use PHP or who already have a special affection for it.

P.S: At Wizeline, Software Engineers can use many technologies and explore our knowledge in different languages, including PHP! If you are interested in growing your knowledge and boosting your career, check our open positions today.

By Sagrario Meneses, Wizeline Senior Software Engineer
By Sagrario Meneses, Wizeline Senior Software Engineer

Aisha Owolabi

Posted by Aisha Owolabi on September 23, 2021