I found myself repeating a lot of the same scripts in HTML with PHP.
I instantiate a class and then call out the properties to echo them to the screen - and in the process make a number of different html files to reflects the differing properties of the class.
In reality, as ultimately, a lot of the scripts are the same, it would be helpful to have the class ‘inspect itself’ to determine which properties are avaible and then echo them to the screen at runtime and I will then just display the data in a list.
I believe that there is something called ‘Reflection’ in PHP for just this. So, instead of of writing the individual properties and values as per below, I can get the class to just generate them on whatever is class is passed to the HTML script at runtime.
The goal would be to pass various types of scripts: $customers / $transactions etc with each of them being a different type of class.
A sample script (without) the reflection might go:
// loop through customers with php foreach $customers as $customer
// print to screen in a html document
<th>$customer->name of property1</th>
<th>$customer->name of property2</th>
<th>$customer->name of property3</th>
<th>$customer->value of property1</th>
<th>$customer->value of property2</th>
<th>$customer->value of property3</th>
… while something with a reflection [or alternative] would just print as many properties and values were avfailable for each row - dependong in which class was instatiated.
I am a little unclear as to how to scrip that and find the documentation a little fuzzy.
Reflection comes in handy in some situations. This is not one of these situations. You could use it, but you would give full control over to PHP. What if there is a password field in there somewhere, should that be exposed as well? Probably not. Or what if a person has a first and last name and you want to combine those into one column? Of what if one of the columns contains a URL to an image?
In cases like these it seems like a good idea to automate everything, until you run into exceptions of the rule and find you’ve painted yourself into a corner.
That is not to say you should keep typing everything by hand. There are ways around this, like adding methods to classes themselves that return information on how to use it in a table, or code that knowledge in another class. All of that can be abstracted and tweaked to your needs in such a way that when the exception comes along your code can accommodate for it.