Implement ReflectionProperty::is{Readable,Writable}()#16209
Draft
iluuu1994 wants to merge 1 commit intophp:masterfrom
Draft
Implement ReflectionProperty::is{Readable,Writable}()#16209iluuu1994 wants to merge 1 commit intophp:masterfrom
iluuu1994 wants to merge 1 commit intophp:masterfrom
Conversation
vudaltsov
reviewed
Oct 3, 2024
iluuu1994
commented
Oct 10, 2024
47d23c6 to
37480d3
Compare
31130cc to
b6eb511
Compare
cab3331 to
325b8fa
Compare
arnaud-lb
reviewed
Feb 5, 2026
| } | ||
| } else if (obj && (!prop->hooks || !prop->hooks[ZEND_PROPERTY_HOOK_GET])) { | ||
| zval *prop_val = OBJ_PROP(obj, prop->offset); | ||
| if (Z_TYPE_P(prop_val) == IS_UNDEF) { |
Member
There was a problem hiding this comment.
The object may need to be initialized if the prop is undef:
Suggested change
| if (Z_TYPE_P(prop_val) == IS_UNDEF) { | |
| if (Z_TYPE_P(prop_val) == IS_UNDEF) { | |
| if (zend_lazy_object_must_init(obj) && !(Z_PROP_FLAG_P(prop_val) & IS_PROP_LAZY)) { | |
| if (!(obj = zend_lazy_object_init(obj)) { | |
| RETURN_THROW(); | |
| } | |
| prop_val = OBJ_PROP(obj, prop->offset); |
Test case:
class A {
public int $prop;
public function __construct() {
$this->prop = 1;
}
}
$rc = new ReflectionClass(A::class);
$obj = $rc->newLazyProxy(fn() => new A());
$rp = new ReflectionProperty(A::class, 'prop');
var_dump($rp->isReadable(null, $obj)); // should be true| } | ||
| RETURN_TRUE; | ||
| } | ||
| RETURN_FALSE; |
Member
There was a problem hiding this comment.
At this point, if the object is lazy it may need to be initialized, and obj->properties must be checked again:
Suggested change
| RETURN_FALSE; | |
| if (zend_lazy_object_must_init(obj)) { | |
| obj = zend_lazy_object_init(obj); | |
| if (!obj) { | |
| RETURN_THROWS(); | |
| } | |
| if (obj->properties && zend_hash_find_ptr(obj->properties, ref->unmangled_name)) { | |
| RETURN_TRUE; | |
| } | |
| } | |
| RETURN_FALSE; |
The initialized object will have neither __get or __isset since the uninitialized one didn't, so we don't need to check these again.
Test case:
#[AllowDynamicProperties]
class A {
public function __construct() {
$this->prop = 1;
}
}
$rc = new ReflectionClass(A::class);
$obj = $rc->newLazyProxy(fn() => new A());
$rp = new ReflectionProperty(new A, 'prop');
var_dump($rp->isReadable(null, $obj)); // should be true| } | ||
| } else if (obj && (prop->flags & ZEND_ACC_READONLY)) { | ||
| zval *prop_val = OBJ_PROP(obj, prop->offset); | ||
| if (Z_TYPE_P(prop_val) != IS_UNDEF && !(Z_PROP_FLAG_P(prop_val) & IS_PROP_REINITABLE)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-15309
Fixes GH-16175
RFC: https://wiki.php.net/rfc/isreadable-iswriteable