Created
March 5, 2020 10:02
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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