Yes. There is a function available. To check if a property exists on an object or class you can use property_exists()
Reference: https://www.php.net/manual/de/function.property-exists.php
I have a function that takes a key and value to set a property on an object. Like below.
public function setValue(string $key, string $value): void
{
// Some extra logic
$this->privateObject->{$key} = $value;
}
I want to be able to enforce that $key is an existing property of a class preferably using PHPStan. I tried using key-of<PrivateObject>, but this does not seem to work with objects.
Currently, I'm manually adding the properties to this function using @phpstan-param 'foo'|"bar"|'baz' $key, but that's so tedious if the object changes.
Does anyone know if this is possible?
Please or to participate in this conversation.