I've read that it's better to have properties of a class be private, and use get/set methods to access/change them.
So I set up a class that way, but I want to be able to display the properties in an html table and I was going to use a separate htmlTable display class to do it.
I thought of 4 possibilities. Feel free to skip them if you already know the ideal way to do this.
Thank you.
Possibilities:
I can get the class fields using:
$class_Vars = get_class_vars($object_class); $fields = $class_vars['fields']; // But as far as iterating through each object, this doesn't work: foreach($object_array as $current_object) { foreach($current_object as $value) { $html = '<td>' . $value . '</td>'; } }The values are private and inaccessible.
A possible solution that looks very clumsy and would probably be a debugging nightmare is:
foreach($fields as $value) { $get_func = 'get' . ucwords($value); // e.g. $get_func = 'getId' $current_value = $current_object->$get_func(); }I think it would work but it doesn't sit right with me.
Interface. Another possibility is to write in an htmlTable function into every class I want to do it. But that is a lot of code reuse.
Interface. Or I could write in an
export()function into every class that just outputs an array with property names and values. Then my htmlTable class can just handle those outputs.
protected, then the classes, which inherit from the original, can access them too.