|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81114 Can't bind_results directly to class properties in strictly_typed constructor
Submitted: 2021-06-09 05:40 UTC Modified: 2021-06-09 06:04 UTC
From: clockwork dot singularity at gmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 7.4.20 OS: Ubuntu
Private report: No CVE-ID: None
 [2021-06-09 05:40 UTC] clockwork dot singularity at gmail dot com
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:

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);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-09 06:04 UTC]
-Status: Open +Status: Not a bug
 [2021-06-09 06:04 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

 [2021-06-09 09:58 UTC]
This is not really a bug in PHP. The language works as intended. What you need to do is make the property nullable or assign a default value. See

> Typed properties must be initialized before accessing, otherwise an Error is thrown.

P.S. I would also recommend PDO over mysqli. If you can't make the switch to PDO yet, then maybe accessing the return values using mysqli_stmt::get_result would be easier and less confusing for you.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Oct 02 19:03:43 2022 UTC