Edit report at https://bugs.php.net/bug.php?id=63591&edit=1
ID: 63591
User updated by: HMWiesinger at gmx dot at
Reported by: HMWiesinger at gmx dot at
Summary: Can't write to properties of mysqli classes using
Reflection API
Status: Not a bug
Type: Bug
Package: MySQLi related
Operating System: Linux
PHP Version: 5.4.9
Block user comment: N
Private report: N
New Comment:
Thanks for the hint. That indeed works like a charm :)
Previous Comments:
------------------------------------------------------------------------
[2012-11-25 04:24:05] [email protected]
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-24 17:28:53] 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-24 16:20:09] [email protected]
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 15:23:48] [email protected]
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-23 20:07:34] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63591&edit=1