PHP Reflection used for HTML Table

Hi,

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

<!doctype>
<html>
<head></head>
<table>
<thead>
<tr>
<th>$customer->name of property1</th>
<th>$customer->name of property2</th>
<th>$customer->name of property3</th>
</tr.
</th>
<tbody>
<tr>
<th>$customer->value of property1</th>
<th>$customer->value of property2</th>
<th>$customer->value of property3</th>
</tr>
</tobdy>
</table>
</html>

… 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.

Any help appreciated!
thanks
Karen

https://www.php.net/manual/en/book.reflection.php

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.

If you can post a link to your repo we can review your code. As mentioned, Reflection is probably not the answer. I suspect you have some room for refactoring your code.

Hi yes, I see your point.

Good point.
I will think about how to use a method in a class that I can call / manage exceptions.

Also, as to putting up the code, as it is buried in a myriad of ways in a framework, it does not easily lend itself to putting up.

Let me think about writing a small class to handle this and if or when to extrapolate from the framework to display some code for further discussion.

Many thanks as always.

In gratitude,
Karen

You do realise that’s normally not a good thing, right?

Especially when something is just used for displaying purposes I don’t see why it should be coupled to any framework at all.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.