How to use strict types with typed properties in PHP

1 Answer

0 votes
declare(strict_types=1);

class Product {
    public int $price;
}

$p = new Product();
$p->price = 150;     // OK
$p->price = "150";   // TypeError



/*
run:

ERROR!

Fatal error: Uncaught TypeError: Cannot assign string to property Product::$price of type int

*/

 



answered Aug 9 by avibootz
...