Bug #81114 [Com]: Can't bind_results directly to class properties in strictly_typed constructor
| From: | dharman@php.net | Date: | Wed, 09 Jun 2021 09:58:30 +0000 |
| Subject: | Bug #81114 [Com]: 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-234275@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
Comment by: dharman@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: 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:
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 https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.typed-properties
> 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.
Previous Comments:
------------------------------------------------------------------------
[2021-06-09 06:04:53] krakjoe@php.net
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.
------------------------------------------------------------------------
[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