Page 1 of 1

Should I use static functions for printing html

Posted: Mon Dec 31, 2007 10:18 pm
by parka
Currently, I'm printing some html by including functions.

Should I create a class for printing html? Thereby, making the functions static.
Like that I can remember where these functions are from.

Because not all my pages using all the functions. Some pages only use one function.
Using static functions is the right move here?

Posted: Mon Dec 31, 2007 10:39 pm
by Christopher
Can you explain what you mean by making your "functions static" ? Maybe show some example code?

Posted: Mon Dec 31, 2007 11:51 pm
by parka
Currently, I'm using this

Code: Select all

function printLink($name, $address){
	echo '<a href="' . $address . '">' . $name . '</a>';
}
Thinking of changing it to this instead:

Code: Select all

class HTMLPrinter{

	static public function printLink($name, $address){
		echo '<a href="' . $address . '">' . $name . '</a>';
	}

}

Posted: Tue Jan 01, 2008 1:45 am
by webspider
Because not all my pages using all the functions. Some pages only use one function.
Using static functions is the right move here?
We get one benefit by making methods static.we can call static methods without creating an object of the class.

Code: Select all

HTMLPrinter::printLink($name, $address);

Posted: Tue Jan 01, 2008 4:20 pm
by Kieran Huggins
Good question Parka!

I'd be more inclined to build this into an XHTML template class so when you run the class (and parse your template) you have access to that subset of methods INSIDE the object, but it won't compete with other template parsers (XML, RSS, etc...)

Does that make sense?

Posted: Tue Jan 01, 2008 4:25 pm
by Christopher
The only difference between the two examples that you showed is that using the class construct uses more memory and is slower. If you want to namespace then just call all the functions in the library like HTMLPrinter_printLink($name, $address).