An easier way to do this? Moving functions to the inside of a class (methods)

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.

It seems as if you want to create a Collection of objects.
http://www.php.net/manual/en/class.arrayaccess.php

Implementing ArrayAccess makes your class accessible like an array. What I would do is have a MessageCollection class that implements ArrayAccess, this collection should only hold Message objects.

EDIT: You may also want to implement the following:

http://www.php.net/manual/en/class.iterator.php - Iterator, so you can loop through the collection

http://www.php.net/manual/en/class.countable.php - Countable, so you can use the count function that of arrays.

An array is probably just fine for what you want to do. But do investigate the foreach control:


foreach($messages as $msg)
{
    if($msg->msg_table == $table)
    {
        $pageContent[$msg->msg_area] .= (!empty($pageContent[$msg->msg_area]))?"<br><br>" . $msg->msg_text:$msg->msg_text;
    } 
}

Eventually you will probably want to move the above bit of code to a template anyways.

I think you’ve got the wrong elements as objects – since you aren’t having any of those elements self-process, the data should be a list – INSIDE and object.

I’m thinking something like:


// FOR REFERENCE 
// $pageContent["msgbar"] = "";  
// $itemized_table_name = "listings"; 
// $table = "listings"; 

class messageHandler {

	private
		messageList=array();
	
	public function addMessage($table,$area,$text) {
	
		$this->messageList[]=array(
			'table' => $displayTable,
			'area' => $displayArea,
			'text' => $text
		);
		
	} // method addMessage
	
	public function parseMessages($table) {
	
		global
			$pageContent;
		
		foreach ($this->messageList as $message) {
			if ($message['table']==$table) {
				$pageContent[$message['area']].=(
					!empty($pageContent[$message['area']]) ? '<br><br> : ''
				).$message['text'];
			}
		}
		
	} // method parseMessages
	
} // class messages

$messages=new messageHandler();
$messages->addMessage($itemized_table_name,'msgbar','&gallery_default_size=editable');
$messages->addMessage('channels','msgbar','Use the preview option to see more.'); 
$messages->addMessage('channels','msgbar','Use the slot field to order pages.'); 

$messages->parseMessages($table);

Though to be honest, I’m not sure I’d be using objects for this as it seems like a waste of overhead and code.


// FOR REFERENCE 
// $pageContent["msgbar"] = "";  
// $itemized_table_name = "listings"; 
// $table = "listings"; 

function parseMessages($table,&$messageList) {
	global
		$pageContent;
	
	foreach ($messageList as $message) {
		if ($message[0]==$table) {
			$pageContent[$message[1]].=(
				!empty($pageContent[$messages[1]]) ? '<br><br> : ''
			).$messages[2];
		}
	}
}

$messages=array(
	array($itemized_table_name,'msgbar','&gallery_default_size=editable'),
	array('channels','msgbar','Use the preview option to see more.'),
	array('channels','msgbar','Use the slot field to order pages.')
); 

parseMessages($table,$messages);

Sometimes it’s just not worth the overhead and extra code of using objects… particularly with PHP’s arrays being so versatile.

Also, notice I’m using foreach – it’s usually a REALLY bad idea to try and trust numeric indexes on PHP arrays when looping… oh, and the above code is untested/not debugged, so there may be a typo or two.

Dearthshadow, that top example is exactly what I’m trying to implement here. Even if the overhead is more expensive, I know I’ve got to get a handle on using classes like that, just to know whether or not it makes sense. I’m going to give it a shot, thanks for the help.