/road-to-php8

Welcome to this repository which showcases some of the interesting features of PHP 8.

Primary LanguagePHP

Road to PHP 8

Welcome to this repository which showcases some of the interesting features of PHP 8.

Topics

  1. Union types
  2. Named arguments
  3. Exception improvements
  4. Match expression
  5. mixed type
  6. Constructor property promotion
  7. Nullsafe operator
  8. Weak maps
  9. Attributes
  10. Object classnames
  11. Stringable interface
  12. New string functions
  13. Trailing comma in function calls

Slides

Slides can be downloaded here.

PHP 8 environment

You can either install PHP 8 locally to run the code examples or use the provided helper script to run the examples inside Docker container.

Installing PHP 8

Add the ondrej/php repository to you Ubuntu machine:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Install only cli package, it should be enough:

sudo apt install php8.0-cli

In case if you need to install some of the php extensions:

sudo apt install php8.0-[extname]

Installation of PHP 8 will set is as default PHP environment on your machine. To set the PHP 7.4 as the default environment, run the following commands:

sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4

Running example in Docker container

Use the run helper script to run the code example inside Docker container:

./run 1_union_types/index.php