I want to give access to protected properties to another class. I have been trying to come up with a clever way of doing this.
I have one class
abstract class Form_Abstract {
protected $formAttributes = array();
protected $fields = array();
}
with setter methods for $fields and $formAttributes. to make it even more complicated, the field setter method creates a separate Field class with its own protected properties and adds it to the fields list. I want to enforce the use of setters to regulate the data that goes into this class.
BUT, I want to be able to pass this class to a variety of other classes, that would crawl the data and use it in different ways. The ways that the other classes would use the data are very different, so it would be hard to do a command type pattern.
Things I have considered:
Using the __get method to access protected properties from outside the class, thus basically making the class read only. This however takes about 350% longer to access the variables.
Having a method that outputs all of the class variables in a multi-dimensional array. While this seems the best way to hide the internals of the class, this doesn't seem very efficient to have to generate these arrays possibly several times per instance of the script.
Doing some sort of inheritance trick which allows access to classes that extend a specific base class or something. This seems like a cool option, but creates dependencies in a way that I would like to avoid. It also requires special methods to get variables.
I could of course make all of the properties public, but this does not allow me to control the data that goes in.
Any ideas or thoughts appreciated.