Really simple OOP question

Hi, this might be a bit of a weird question but I’m trying to follow best coding practises when it comes to OOP development and am wondering how (in terms of OOP) you would go about populating a drop down list with data out of the below examples.

From my understanding OOP is about being modular so that would include seperating presentation (html etc) from the class, so I would imagine it would look something like this?



exampleclass.php

class ExampleClass {

// Misc methods etc

public function getGroupList() {
 return  $this->db->query("SELECT title, gid FROM groups");
}

}


example.php

<select id="group_id" name="group_id">
$res = $exampleClass->getGroupList();
while ($row = $db->fetchAssoc($res)) {
echo "<option value=\\"" . $row['gid'] . "\\">" . $grow['title'] . "</option>\
";
}
</select>


Or would you just put all of the html in a getGroupList method and call that?

Many thanks in advance!

In this case, I would create one function which has all of the select HTML, which can then be returned.

If I might need to use the getGroupList() more than once, I’d keep that a separate function (as it is now). If not, I’d merge it all into one function.

It’s all about re-usability. A simplistic (but effective) rule to remember: If you have to write the same thing more than once, there is probably a better way to do it.

That last rule of thumb is an interesting one to remember.

As it stands I will require the list more than once, but it will always be in a select format so I will add the entire select html to a method which returns the html.

I just find it tough sometimes as to when to not put the html in a method and when it would be appropriate to do so.

Does anyone else have an opinion on this?

Basically, the rule is if you need to use it more than once, put it in a method. =p

Also, it also depends on your application. For example, if I’m working on a Wordpress or similar theme, a lot of my HTML goes outside of functions (especially stuff I only use once, or things where I put them in files I include later).

However, if I’m working on something with a MVC (model-view-controller) structure, where my HTML gets included via function calls, I’ll frequently put all my HTML into functions. This gives an added perk of being able to override this function in a child class and changing the HTML that is output just for a specific aspect.

You can still separate the ‘getting from the DB’ from the ‘making a select list’. That way if you want to get from the DB, without presenting as a <select> you can access the one method and not the other.

Example, both behaviours done by different methods of the same class



class ExampleClass
{
	
	public function getGroupList() {
		if($result = $this->db->query("SELECT title, gid FROM groups")) {
			return $result;
		}
		return null;
		
		/* maybe return false if there was an error with query
		as opposed to empty result set*/
	}
	
	
	public function groupSelect() {
		if(!$groups = $this->getGroupList()) {
			return 'There are no groups';
		}
		
		$select = "<select id='group_id' name='group_id'>\
";
		foreach($groups as $g) {
			$select .= "<option value='$g[gid]'>" . htmlspecialchars($g['title']) . "</option>\
";
		}
		
		return $select . "</select>";
	}
}


$groupObj = new ExampleClass();

//Select list
echo $groupObj -> groupSelect();


//Some other use of the data
//shares the same data access method
if($groups = $groupObj -> getGroupList()) {
	echo "<ul>";
	foreach($groups as $g) {
		echo "<li>$g[title]</li>\
";
	}
	echo "</ul>";
}

There is some separation, but the same class is doing business and presentation logic. For this little example it’s okay IMO. If things were more complex, and you had different data access methods, I would separate them further.

I’d say your object probably shouldn’t know what HTML is. It knows what is in its list.

Think about having another class which knows how to “do” HTML. Imagine a getSelect() method which takes an id and array (or iterator, whatever) to give you a string with all the HTML for a select. Then all you’d need is something like:


HTMLClass::getSelect( 'group_id', $exampleClass->getGroupList() );

and if you wanted an unordered list somewhere else:


HTMLClass::getUl( $exampleClass->getGroupList() );