Skip to content

Instantly share code, notes, and snippets.

@woprrr
Created March 5, 2020 10:02
Show Gist options
  • Save woprrr/bd0ee94c740f5b8f7ab2d455dd5c3cc2 to your computer and use it in GitHub Desktop.
Save woprrr/bd0ee94c740f5b8f7ab2d455dd5c3cc2 to your computer and use it in GitHub Desktop.
PHP 7.4 Typed Properties examples. Try it now : https://3v4l.org/iFsCM
<?php
# Typed Properties 2.0
# OLD WAY (PHP < 7.4 )
class User {
public $id;
public $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
$user = new User(10, []); // This Will throw a Fatal error.
# Typed Properties with PHP 7.4 All types are supported, with the exception of void and callable.
class NewUser {
public int $id;
public string $name;
}
$newUser = new NewUser;
$newUser->id = 10;
$newUser->name = []; // This Will throw a Fatal error too !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment