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

Allow option to use dynamic properties for specific classes #602

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcelthole
Copy link

Hey,
i have a special use case where i need to allow dynamic properties via __get and __set in a class.
But with the strict type handling this package will not allow this, because all parameters must be known already.

So i created this PR for discussion if this solution would be fine for you.
The next idea could also be to check for the #[\AllowDynamicProperties] class attribute - But this will takle a little bit other problem, where you don't need the __get and __set at all.

@marcelthole
Copy link
Author

I have still one isse and need your help here. I would like to use also generics for this implementation

/**
 * @template TValue
 * @extends \ArrayAccess<string, TValue>
 */
interface DynamicProperties extends \ArrayAccess {
    /** @return list<string> */
    public function getAdditionalPropertyKeys(): array;
    /** @return TValue */
    public function __get(string $name): mixed;
    /** @param TValue $value */
    public function __set(string $name, mixed $value): void;
    public function __unset(string $name): void;
    public function __isset(string $name): bool;
}

No i also want to use this strict types here and to validate the given values. Currently this implementation use the given type and i can't find a way to get the implements (or extends if i wrap it in an abstract class) TValue - or is it easier to get the type from the __set method signature? But this is without the TValue also only mixed

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

Successfully merging this pull request may close these issues.

1 participant