-
Notifications
You must be signed in to change notification settings - Fork 145
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
[FR] Assert property initialized #218
Comments
Oh, |
Since we can't access the property before we know it's initialized I see only this way class X {
public ?string $a;
}
function isInit(object $object, string $prop) : void {
$rp = new ReflectionProperty($object::class, $prop);
if($rp->isInitialized($object)) {
return;
}
throw new Exception('Not init');
}
$x = new X;
$x->a = null;
isInit($x, 'a');
$x = new X;
isInit($x, 'a'); |
Maybe instead of this, it could be written as |
@ruudk have you tried it? |
@zerkms You're right. However, in my use case, using Doctrine entities with autoincrement ids, this was fine as I just want to make sure there is a value. class Entity {
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
public function getId(): int
{
Assert::true(isset($this->id));
return $this->id;
}
} |
@ruudk |
@zerkms |
@ruudk if it can never be null - why do you check it with |
It's a long time since I opened the FR and I did not find the use case I needed it for. But yes, one can't use |
Would it make sense to add a new assertion that would check that property is initialized?
The text was updated successfully, but these errors were encountered: