Skip to content

Instantly share code, notes, and snippets.

@woprrr
Created March 5, 2020 10:02
Show Gist options
  • Select an option

  • Save woprrr/bd0ee94c740f5b8f7ab2d455dd5c3cc2 to your computer and use it in GitHub Desktop.

Select an option

Save woprrr/bd0ee94c740f5b8f7ab2d455dd5c3cc2 to your computer and use it in GitHub Desktop.

Revisions

  1. Alexandre Mallet created this gist Mar 5, 2020.
    25 changes: 25 additions & 0 deletions gist.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?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 !!