Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to hydrate private props from parent classes #5

Open
veewee opened this issue Jan 31, 2024 · 0 comments
Open

Make it possible to hydrate private props from parent classes #5

veewee opened this issue Jan 31, 2024 · 0 comments

Comments

@veewee
Copy link
Owner

veewee commented Jan 31, 2024

Feature Request

Q A
New Feature yes
RFC yes/no
BC Break yes/no

Summary

See https://3v4l.org/WQtLD

<?php

abstract class X {
    private string $x;
}

#[AllowDynamicProperties]
class Y extends X {}

$rc = new ReflectionClass(Y::class);
try {
    var_dump($rc->getProperties());
    var_dump($rc->getProperty('x'));
} catch (\Exception $e) {
    echo $e .PHP_EOL.PHP_EOL;
}

$y = new Y;

// Solution is to fetch prop from parent:
$xProp = $rc->getParentClass()->getProperty('x');
var_dump($xProp->setValue($y, 'hello'));

// Since its a dynamic class, fallback is:
    
$y->x = 1;
var_dump($y, $y->x);

// Which is ... odd :)

array(0) {
}
ReflectionException: Property Y::$x does not exist in /in/WQtLD:13
Stack trace:
#0 /in/WQtLD(13): ReflectionClass->getProperty('x')
#1 {main}

NULL
object(Y)#3 (2) {
["x":"X":private]=>
string(5) "hello"
["x"]=>
int(1)
}
int(1)

The root cause for this is this:
https://3v4l.org/kvVm0

class A {
    private string $id;
}

class B extends A {
    private int $id;
}
class C extends B {
    protected float $id;
}
class D extends C {
    public float $id;
}


$a = new A();
$b = new B();
$c = new C();
$d = new D();
var_dump($a, $b, $c, $d);

object(A)#1 (0) {
["id":"A":private]=>
uninitialized(string)
}
object(B)#2 (0) {
["id":"A":private]=>
uninitialized(string)
["id":"B":private]=>
uninitialized(int)
}
object(C)#3 (0) {
["id":"A":private]=>
uninitialized(string)
["id":"B":private]=>
uninitialized(int)
["id":protected]=>
uninitialized(float)
}
object(D)#4 (0) {
["id":"A":private]=>
uninitialized(string)
["id":"B":private]=>
uninitialized(int)
["id"]=>
uninitialized(float)
}

So to decide what to do with hydration : keep it simple and only run the action on the property that is directly available.
Or : trigger hydrating the property on all known props (on current and parents parents).
Not sure what makes sense here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant