php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: HMWiesinger at gmx dot at
New email:
PHP Version: OS:

 

 [2012-11-23 20:07 UTC] HMWiesinger at gmx dot at
Description:
------------
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->setAccessible(TRUE);
$property->setValue($mysqli, 10);


Expected result:
----------------
No Error

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-24 15:23 UTC] reeze@php.net
-Status: Open +Status: Feedback
 [2012-11-24 15:23 UTC] reeze@php.net
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] laruence@php.net
-Status: Feedback +Status: Not a bug
 [2012-11-24 16:20 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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] laruence@php.net
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-2022 The PHP Group
All rights reserved.
Last updated: Wed Aug 17 08:05:44 2022 UTC