I’ve got a very basic class built, with a constructor, and then I make an array of these different objects. Then I iterate through them and if conditions match, I spit out the data that I want.
Instead of make an array of these different objects, wouldn’t it be great if the object housed it’s own array, and then had its own function to iterate through everything just w/a simple method call. If there’s any sharp PHP minds w/solid experience w/classes & objects, would you help me streamline what I’ve got into one decent class?
// FOR REFERENCE
// $pageContent["msgbar"] = "";
// $itemized_table_name = "listings";
// $table = "listings";
class msg
{
function __construct($display_table, $display_area, $text)
{
$this->msg_table = $display_table;
$this->msg_area = $display_area;
$this->msg_text = $text;
}
}
$messages = array();
$messages[] = new msg($itemized_table_name, "msgbar", "&gallery_default_size=editable");
$messages[] = new msg("channels", "msgbar", "Use the preview option to see more.");
$messages[] = new msg("channels", "msgbar", "Use the slot field to order pages.");
for($x = 0; $x < sizeof($messages); $x++)
{
if($messages[$x]->msg_table == $table)
{
$pageContent[$messages[$x]->msg_area] .= (!empty($pageContent[$messages[$x]->msg_area]))?"<br><br>" . $messages[$x]->msg_text:$messages[$x]->msg_text;
}
}
Ideally, this would be what happens:
$messages is initialized a new instance of “msg”. And I do something like $messages->add_msg(“table”, “msg_area”, “msg_text goes here”. That would save it to an array stored inside of $messages. Then a call of $messages->compile_msgs() would check if the current table matches a table-specific message and add it to a global variable on the outside if it does.
I appreciate any knowledge that you guys who are experienced in this can pass on to me.