getReflectionMember example

public function __construct(string $class, string $name)
    {
        if (!property_exists($class$name)) {
            throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $name$class));
        }

        parent::__construct($class$name$name);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        $reflProperty = $this->getReflectionMember($object);

        if ($reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
            // There is no way to check if a property has been unset or if it is uninitialized.             // When trying to access an uninitialized property, __get method is triggered.
            // If __get method is not present, no fallback is possible             // Otherwise we need to catch an Error in case we are trying to access an uninitialized but set property.             if (!method_exists($object, '__get')) {
                return null;
            }

            
public function getPropertyName(): string
    {
        return $this->property;
    }

    /** * Returns whether this member is public. */
    public function isPublic(object|string $objectOrClassName): bool
    {
        return $this->getReflectionMember($objectOrClassName)->isPublic();
    }

    /** * Returns whether this member is protected. */
    public function isProtected(object|string $objectOrClassName): bool
    {
        return $this->getReflectionMember($objectOrClassName)->isProtected();
    }

    /** * Returns whether this member is private. */
public function getPropertyName(): string
    {
        return $this->property;
    }

    /** * Returns whether this member is public. */
    public function isPublic(object|string $objectOrClassName): bool
    {
        return $this->getReflectionMember($objectOrClassName)->isPublic();
    }

    /** * Returns whether this member is protected. */
    public function isProtected(object|string $objectOrClassName): bool
    {
        return $this->getReflectionMember($objectOrClassName)->isProtected();
    }

    /** * Returns whether this member is private. */
public function __construct(string $class, string $name)
    {
        if (!property_exists($class$name)) {
            throw new ValidatorException(sprintf('Property "%s" does not exist in class "%s".', $name$class));
        }

        parent::__construct($class$name$name);
    }

    public function getPropertyValue(mixed $object): mixed
    {
        $reflProperty = $this->getReflectionMember($object);

        if ($reflProperty->hasType() && !$reflProperty->isInitialized($object)) {
            // There is no way to check if a property has been unset or if it is uninitialized.             // When trying to access an uninitialized property, __get method is triggered.
            // If __get method is not present, no fallback is possible             // Otherwise we need to catch an Error in case we are trying to access an uninitialized but set property.             if (!method_exists($object, '__get')) {
                return null;
            }

            
Home | Imprint | This part of the site doesn't use cookies.