|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63591 Can't write to properties of mysqli classes using Reflection API
Submitted: 2012-11-23 20:07 UTC Modified: 2012-11-28 08:36 UTC
From: HMWiesinger at gmx dot at Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.4.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: HMWiesinger at gmx dot at
New email:
PHP Version: OS:


 [2012-11-23 20:07 UTC] HMWiesinger at gmx dot at
It doesn't seem to be possible to get write access to properties of mysqli classes 
using the Reflection API. 

Test script:
$mysqli = new MySQLi();
$reflection = new ReflectionClass('\MySQLi');

$property = $reflection->getProperty('affected_rows');
$property->setValue($mysqli, 10);

Expected result:
No Error

Actual result:
Fatal error: ReflectionProperty::setValue(): Cannot write property


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-24 15:23 UTC]
-Status: Open +Status: Feedback
 [2012-11-24 15:23 UTC]
affected_rows property is kinda readonly property, so we can't change
that with reflection apis or property value update. 

it makes the property reliable, I don't see a reason to change that,
I you really need to do that inherit it with a subclass.
 [2012-11-24 16:20 UTC]
-Status: Feedback +Status: Not a bug
 [2012-11-24 16:20 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

mysqli link properties are designed to be read-only.

the write func for them throw that error:

static int mysqli_write_na(mysqli_object *obj, zval *newval TSRMLS_DC)
    php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot write property");
    return FAILURE;
 [2012-11-24 17:28 UTC] HMWiesinger at gmx dot at
I understand that making the properties (not only affected_rows) readonly 
ensures certain integrity. No arguing there.

What I'm trying to do is mock the MySQLi class (and some others) for unit 
testing. Mocking the methods works fine, but the properties have been a bit of a 
difficulty. My solution so far was to hook in behind MySQLi and mock the actual 
database connection using mysqlnd_uh. But that is not working (yet) with 5.4 and 
no real estimations of when that will be fixed. With 5.5 drawing nearer, I was 
looking at alternative ways to do this. I tried subclassing MySQLi as well, that 
didn't work either.
 [2012-11-25 04:24 UTC]
hmm, yes, subclass still use that write_func.

a solution might be use composite, like:

class MysqliMock {
   public $mysqli;
   public function __construct() {
        $this->mysqli = new MySQLi();
   public function __call($method, $args) {
        call_user_func_array(array($this->mysqli, "method"), $args)

   public function __get($name) {
     //you can mock the properties here

   public function __set($name, $value) {


what do you think? thanks
 [2012-11-28 08:36 UTC] HMWiesinger at gmx dot at
Thanks for the hint. That indeed works like a charm :)
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Dec 08 20:01:28 2023 UTC