Bug #81114 [NEW]: Can't bind_results directly to class properties in strictly_typed constructor
| From: | clockwork dot singularity at gmail dot com | Date: | Wed, 09 Jun 2021 05:40:26 +0000 |
| Subject: | Bug #81114 [NEW]: Can't bind_results directly to class properties in strictly_typed constructor | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-234272@lists.php.net to get a copy of this message | ||
From: clockwork dot singularity at gmail dot com
Operating system: Ubuntu
PHP version: 7.4.20
Package: MySQLi related
Bug Type: Bug
Bug description:Can't bind_results directly to class properties in strictly_typed constructor
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 bug report at https://bugs.php.net/bug.php?id=81114&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81114&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81114&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81114&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81114&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81114&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81114&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81114&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81114&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81114&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81114&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81114&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81114&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81114&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81114&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81114&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81114&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81114&r=mysqlcfg