I'm fairly new to using traits in PHP and am wondering if there is a way to ensure that a class including a trait has particular properties.
I know that you can use something like the following to ensure the using class has particular methods.
protected abstract function some_function();
Is there a way to do the same kind of thing for member variables, since a trait has access to the member variables in the using class?
I can just document the requirement, but it would be nice to throw an exception in case a class trying to use a trait is missing a required property.
The closest that I can find is to create a function in the trait that attempts to grab the variable and return it ... and throw an exception if it is not found. Then the methods in the trait can use that to access the parent classes property instead of accessing them directly.
Thoughts, suggestions, and recommendations welcome ... as I am just getting used to traits.
The use case is that I have certain things that only run on the public face of the site and certain other things that only run on the private face of the site, but both share a common set of base functionality (various configuration settings, error handling, etc).
I could avoid using traits by making the class files for each of the inherited classes extremely long, but I'm trying to keep like functionality together in smaller files if possible.
I suppose one possibility would be to require an accessor variable in the calling class to return the wanted property.
abstract function get_my_value();
Anything else that might work cleanly?
Thanks in advance!