Bug #81114 [Opn->Nab]: Can't bind_results directly to class properties in strictly_typed constructor
| From: | krakjoe@php.net | Date: | Wed, 09 Jun 2021 06:04:56 +0000 |
| Subject: | Bug #81114 [Opn->Nab]: Can't bind_results directly to class properties in strictly_typed constructor | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234273@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81114&edit=1
ID: 81114
Updated by: krakjoe@php.net
Reported by: clockwork dot singularity at gmail dot com
Summary: Can't bind_results directly to class properties in
strictly_typed constructor
-Status: Open
+Status: Not a bug
Type: Bug
Package: MySQLi related
Operating System: Ubuntu
PHP Version: 7.4.20
Block user comment: N
Private report: N
New Comment:
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 http://www.php.net/support.php 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.
Previous Comments:
------------------------------------------------------------------------
[2021-06-09 05:40:26] clockwork dot singularity at gmail dot com
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);
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81114&edit=1