[php-src] Issue #11547: cast_object handler is never called for array cast
| From: | ju1ius | Date: | Tue, 27 Jun 2023 22:24:07 +0000 |
| Subject: | [php-src] Issue #11547: cast_object handler is never called for array cast | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244826@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11547
Author: ju1ius
### Description
The following code:
```c
static zend_class_entry *foo_ce;
static zend_object_handlers foo_handlers;
PHP_MINIT_FUNCTION(my_extension) {
// Class declaration boilerplate omitted
foo_ce = register_class_Foo();
// object handlers initialization boilerplate omitted
foo_handlers.cast_object = foo_cast_object;
foo_ce.default_object_handlers = &foo_handlers;
}
static zend_result foo_cast_object(zend_object *readobj, zval *writeobj, int type) {
switch (type) {
case IS_LONG:
case _IS_NUMBER:
ZVAL_LONG(writeobj, 42);
return SUCCESS;
case IS_ARRAY:
ZVAL_LONG(writeobj, 42);
convert_to_array(writeobj);
return SUCCESS;
default:
return FAILURE;
}
}
```
```php
<?php
$foo = new Foo;
var_dump((int)$foo);
var_dump((array)$foo);
```
Resulted in this output:
```
int(42)
array(0) {
}
```
But I expected this output instead:
```
int(42)
array(1) {
[0]=>
int(42)
}
```
So it seems that the VM unconditionally uses the result of
object->handlers->get_properties_for() without checking beforehand if
object->handlers->cast_object() returns something.
Not _exactly_ a bug per se, but rather yet another quirk whose fixing would improve the usability
the extension API, IMO.
### PHP Version
8.3-dev
### Operating System
_No response_