PHP has been updated with quite a few syntax options in recent years. Let's highlight some of them and see if you're using them.
class Post { public string $title;}
The property $title
will have to be only a string
.
A second new feature introduced in PHP 7.4 is the arrow function. It is a shorter version to write closures. We already discussed this feature in a previous lesson about callbacks
.
use Illuminate\Database\Eloquent\Builder; public function table(Table $table): Table{ return $table ->modifyQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes());}
PHP 8 introduced Attributes. For example, Livewire uses Attributes to use some features like add validation rules.
use Livewire\Attributes\Rule;use Livewire\Component;use App\Models\Post; class CreatePost extends Component{ #[Rule('required|min:3')] public $title = ''; #[Rule('required|min:3')] public $content = ''; // ...}
Constructor properties allow to declare properties right in the constructor class.
Until PHP 8, we had to write like this:
class User { private string $name; public function __construct($name) { $this->name = $name; }}
Now, we can write like this:
class User { public function __construct(private string $name) {}}
Union types allow to have more than one type of declaration.
abstract class BasePage extends Component{ protected ?string $heading = null; public function getHeading(): string | Htmlable { return $this->heading; }}
The null-safe operator provides a safe way to return property, method value, or null.
$invoiceNumber = $order?->invoice?->number;
Match expressions is a new syntax for writing switch statements.
Until PHP 8, the switch would look like this:
public function canAccessPanel(Panel $panel): bool{ switch ($panel->getId()) { case 'admin': return $this->is_admin; break; case 'student': return ! $this->is_admin; break; default: return false; break; }}
Now, using the match expression, we can write like so:
public function canAccessPanel(Panel $panel): bool{ return (bool) match ($panel->getId()) { 'admin' => $this->is_admin, 'student' => ! $this->is_admin, default => false, };}
PHP 8.1 added support for Enumerations, or an Enum for short. Enums can also be type-hinted.
enum Role: int{ case ADMINISTRATOR = 1; case COMPANY_OWNER = 2; case CUSTOMER = 3; case GUIDE = 4;}
Support for read-only properties where property cannot be modified.
class GlobalSearchResult{ public function __construct( readonly public string | Htmlable $title, readonly public string $url, readonly public array $details = [], readonly public array $actions = [], ) { }}
If a class has only read-only properties, the whole class can now be defined as read-only.
readonly class GlobalSearchResult{ public function __construct( public string | Htmlable $title, public string $url, public array $details = [], public array $actions = [], ) { }}
Constants will now be able to be type hinted.
class Role extends Model{ public const int ROLE_ADMINISTRATOR = 1; public const int ROLE_OWNER = 2; public const int ROLE_USER = 3;}
One common issue is the different PHP versions in the Terminal and website. It is possible to install several different PHP versions, and command line interface (CLI) PHP versions differ from your site's version.
You need the same PHP version on the CLI and the site to get things running smoothly.
php -v
PHP 8.0.29 (cli) (built: Jun 8 2023 15:24:43) ( NTS )Copyright (c) The PHP GroupZend Engine v4.0.29, Copyright (c) Zend Technologies with Zend OPcache v8.0.29, Copyright (c), by Zend Technologies
Using the grep
command, you can quickly identify each site's PHP version.
grep -H fastcgi_pass /etc/nginx/sites-enabled/*
From the socket in the output, we can see that they all run on PHP 8.1.
/etc/nginx/sites-enabled/web1.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;/etc/nginx/sites-enabled/web2.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;/etc/nginx/sites-enabled/web3.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;/etc/nginx/sites-enabled/web4.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;/etc/nginx/sites-enabled/web5.test: fastcgi_pass unix:/opt/php/php8.1-fpm.sock;