0

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.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • Try checking out this question: http://stackoverflow.com/questions/317835/php-equivalent-of-friend-or-internal – Adrian Marinica Jun 02 '11 at 23:29
  • @AdrianMar Yeah that's similar to what I have already considered, but as mentioned in the original post, __get is VERY slow. – dqhendricks Jun 02 '11 at 23:38
  • Sorry, must have jumped over that row when reading. Well, beside extending the main class as suggested below, I see no other straight forward alternative. Good luck! – Adrian Marinica Jun 02 '11 at 23:41

1 Answers1

1

I'm probably missing something, but it seems to me that all you want to do is extend the Form_Abstract class...

abstract class Form_Abstract {
    protected $formAttributes = array('apple', 'orange');
    protected $fields = array();
}

class My_Form extends Form_Abstract
{
    function __construct() { }

    function displayFormAttribute($num)
    {
        return $this->formAttributes[$num];
    }
}

$x = new My_Form;

echo $x->displayFormAttribute(0); // apple
echo $x->displayFormAttribute(1); // orange

Extending gives you access to all of the public and protected variables and functions, however not the private ones.

Adrian Marinica
  • 2,191
  • 5
  • 29
  • 53
tplaner
  • 8,363
  • 3
  • 31
  • 47
  • yeah, not exactly what I am looking for. there will be a bunch of classes that extend this abstract class, and I want to be able to run any one of them through several other classes without necessarily coupling them. for instance, a request validation class that does not necessarily need a form to run, but can use a form to auto generate validations. I don't want to have to include all the bulk of a form in the request validator class just so that it can access another forms vairables. – dqhendricks Jun 02 '11 at 23:27
  • i have decided it is best to make everything public. thanks for the help though! – dqhendricks Jun 03 '11 at 21:39