Should I use static functions for printing html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
parka
Forum Commoner
Posts: 52
Joined: Mon Feb 26, 2007 6:48 am

Should I use static functions for printing html

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Can you explain what you mean by making your "functions static" ? Maybe show some example code?
(#10850)
parka
Forum Commoner
Posts: 52
Joined: Mon Feb 26, 2007 6:48 am

Post 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>';
	}

}
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post 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);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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).
(#10850)
Post Reply