|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-06-09 06:04 UTC] krakjoe@php.net
-Status: Open
+Status: Not a bug
[2021-06-09 06:04 UTC] krakjoe@php.net
[2021-06-09 09:58 UTC] dharman@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 08:00:01 2025 UTC |
Description: ------------ I have a class which initializes itself from a database. This works entirely fine normally, but when I `declare(strict_types=1)`, I get this lovely type error instead: `Uncaught Error: Cannot access uninitialized non-nullable property MyClass::$name by reference in MyClass.php:110` While this is technically correct, it feels like the type system is being too aggressive in this situation. My current workarounds are to either: - Not declare strict_types in my models - or bind to local variables, then assign the properties from there (This isn't ideal, as I have ~20 properties, and introduces an additional point of human error) Test script: --------------- declare(strict_types=1); class ExampleModel { public int $id; public string $name; public function __construct($id) { $db = new mysqli(); $stmt = $db->prepare("SELECT name FROM table WHERE id = ?"); $stmt->bind_param('d', $id); $stmt->bind_result($this->name); } }