Page 1 of 2
Going overboard with classes?
Posted: Fri Jul 07, 2006 8:02 pm
by Benjamin
What is the deciding factor when determining whether to use a class or a function. It's so much easier to call a function for something like this...
Code: Select all
class newToolTip {
public $ToolTip;
function PlainTextToolTip($PageText, $ToolTipText, $CSSClass) {
$ToolTip = '<span class="' . $CSSClass . '" '
. 'onMouseOver="'
. "this.T_BGCOLOR='" . TT_BGCOLOR . "'; "
. "this.T_BORDERWIDTH='" . TT_BORDERWIDTH . "'; "
. "this.T_BORDERCOLOR='" . TT_BORDERCOLOR . "'; "
. "this.T_DELAY='" . TT_DELAY . "'; "
. "this.T_FONTCOLOR='" . TT_FONTCOLOR . "'; "
. "this.T_FONTFACE='" . TT_FONTFACE . "'; "
. "this.T_FONTSIZE='" . TT_FONTSIZE . "'; "
. "this.T_FONTWEIGHT='" . TT_FONTWEIGHT . "'; "
. "this.T_PADDING='" . TT_PADDING . "'; "
. "this.T_STATIC='" . TT_STATIC . "'; "
. 'return escape(\'' . $ToolTipText . '\');">'
. $PageText . '</span>';
$this->ToolTip = $ToolTip;
}
function LinkToolTip($Link, $CSSClass, $Target, $ToolTipText, $PageText) {
$ToolTip = '<a href="' . $Link . '" class="' . $Class . '" target="' . $Target . '" onMouseOver="'
. "this.T_BGCOLOR='" . TT_BGCOLOR . "'; "
. "this.T_BORDERWIDTH='" . TT_BORDERWIDTH . "'; "
. "this.T_BORDERCOLOR='" . TT_BORDERCOLOR . "'; "
. "this.T_DELAY='" . TT_DELAY . "'; "
. "this.T_FONTCOLOR='" . TT_FONTCOLOR . "'; "
. "this.T_FONTFACE='" . TT_FONTFACE . "'; "
. "this.T_FONTSIZE='" . TT_FONTSIZE . "'; "
. "this.T_FONTWEIGHT='" . TT_FONTWEIGHT . "'; "
. "this.T_PADDING='" . TT_PADDING . "'; "
. "this.T_STATIC='" . TT_STATIC . "'; "
. 'return escape(\'' . $ToolTipText . '\');">'
. $PageText . '</a>';
$this->ToolTip = $ToolTip;
}
function SetDefaults() {
$this->CSSClass = 'default';
$this->Target = 'self';
}
function DisplayToolTip() {
return $this->ToolTip;
}
}
$ToolTip = new newToolTip();
$ToolTip->PlainTextToolTip('Here is the tooltip.', 'Here is the actual tooltip', 'TheCSSClass');
$TheToolTip = $ToolTip->DisplayToolTip();
echo $TheToolTip;
Re: Going overboard with classes?
Posted: Fri Jul 07, 2006 8:27 pm
by Roja
astions wrote:What is the deciding factor when determining whether to use a class or a function. It's so much easier to call a function for something like this
I vote neither. I put all of that output into a template.
Posted: Fri Jul 07, 2006 8:35 pm
by Benjamin
Well I might have 30 words in a paragraph that need tooltips, so that being the case, besides parsing the text, wouldn't a simple function call be easier?
Edit: Maybe you have an example?
Posted: Fri Jul 07, 2006 8:42 pm
by Roja
astions wrote:Well I might have 30 words in a paragraph that need tooltips, so that being the case, besides parsing the text, wouldn't a simple function call be easier?
Easier in which sense?
Easier to change the code? I'd find the template easier than digging around processing code to find output code, and tweak the speciifc sections that I need to fix to repair my page.
Easier to call? I find calling a template once much easier than having a messy collection of output functions. The output lives in its own little directory, and I only have to deal with it when I need to touch output.
Easier to categorize? As you note, whats the line between functions and classes for this? Classes might make it easier to write a test case, but doesn't offer much in the way of reusability or polymorphism. Functions seem to fit, but isnt the page output an object ("The page")? Templates, on the other hand, are crystal clear.
Easier in which sense?
Posted: Fri Jul 07, 2006 8:48 pm
by Benjamin
Had to use code tags as PHP rendered it all in red..
Easier in this sense...
Code: Select all
"Lorem ipsum dolor sit amet, <?php CreateToolTip('consectetur', 'Here is some tooltip text', 'TheCSSClass'); ?> adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Rather than this, for each word...
Code: Select all
"Lorem ipsum dolor sit amet, <?php
$ToolTip = new newToolTip();
$ToolTip->PlainTextToolTip('consectetur', 'Here is some tooltip text', 'TheCSSClass');
$TheToolTip = $ToolTip->DisplayToolTip();
?> adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Posted: Fri Jul 07, 2006 8:56 pm
by Jenk
I wouldn't have designed it like that.. I'd have a database table for tooltips, with a foreign key to the word/phrase/DOM object they are relevant to, and have it dynamically output.. very, very little data should be static (hard coded) data imo.
Posted: Fri Jul 07, 2006 9:02 pm
by Benjamin
Yeah, that would be the best way to go in this situation. What I really wanted to know though is how do you determine whether something should be in a class or not.
Posted: Fri Jul 07, 2006 9:11 pm
by Jenk
Whether or not the particular section of code your are dealing with fits into a
Pattern
Very little will not be in a class, if you follow strict MVC designs and go full on OO.
Posted: Fri Jul 07, 2006 9:30 pm
by sweatje
How about the best of both worlds:
Code: Select all
function CreateToolTip($word, $tip, $cssclass) {
$ToolTip = new newToolTip();
$ToolTip->PlainTextToolTip($word, $tip, $cssclass);
$TheToolTip = $ToolTip->DisplayToolTip();
}
Posted: Fri Jul 07, 2006 9:33 pm
by Benjamin
sweatje wrote:How about the best of both worlds:
I thought about that as well, but I didn't know if that was practical or not. I'll read up on the MVC.
Posted: Fri Jul 07, 2006 11:44 pm
by alvinphp
The point of classes is organization and if your entire site only had only 4 functions then you could probably just create your 4 functions instead of a class with 4 methods. If you have 400 functions though you probably do not want functions scattered all over the place. You would want them in classes.
Posted: Sat Jul 08, 2006 1:04 am
by AshrakTheWhite
the way my OOP is designed is every page to be a seperate class and to use the Smarty template engine (sorta like PHP INSIDE of HTML) and the superclasses (new word ey?) to have all the SQL querys
aka:
Main.php
Main.tpl
Main_AddTeam.php/tpl
Main_AddObject.php/tpl
all the querys that are called are inside Main.php if the class calling the query has a Main prefix
Posted: Sat Jul 08, 2006 1:57 am
by matthijs
I know the question is about using a class vs a function. But still I would like to ask: would it not be possible to completely remove the php? Just use html templates combined with js and css files? This is something I'm always wondering if I see big chunks of html, css and js being echo'd by phpclasses and/or functions. And then, if one needs a bit of logic, add a small bit of php or custom syntax (smarty-like) to the templates.
As I see it, OOP design should help to make code reusable and flexible, seperating responsibilities (f.e. MVC), etc. It's the same as with seperation of html, css and js. Mixing everything up, complete with css/js/html would seem to go against that.
I haven't been developing large projects yet, so if I'm missing something please educate me.
Re: Going overboard with classes?
Posted: Sat Jul 08, 2006 1:25 pm
by Christopher
astions wrote:What is the deciding factor when determining whether to use a class or a function. It's so much easier to call a function for something like this...
It is so much easier because you are just sticking a couple of functions in a class construct. The result is just more code. They are still functions. Whether you code this as a class or a function library, the design issues are the same.
Do you have any ideas on how to make this into a OO class?
Do you see any places where you could reduce duplicate code?
Do you see any places where you could remove external dependencies?
Posted: Sat Jul 08, 2006 8:25 pm
by Benjamin
Like this?
Code: Select all
class newToolTip {
public $ToolTip;
function newToolTip() {
$this->TT_BGCOLOR = "#ffffe7";
$this->TT_BORDERWIDTH = "1px";
$this->TT_BORDERCOLOR = "#000000";
$this->TT_DELAY = "1";
$this->TT_FONTCOLOR = "#000000";
$this->TT_FONTFACE = "verdana, helvetica, sans-serif";
$this->TT_FONTSIZE = "12px";
$this->TT_FONTWEIGHT = "normal";
$this->TT_PADDING = "5px";
$this->TT_STATIC = "true";
$this->CSSClass = 'default';
$this->Target = 'self';
}
function setBackgroundColor($BGColor) {
$this->TT_BGCOLOR = $BGColor;
}
function setBorderWidth($BorderWidth) {
$this->TT_BORDERWIDTH = $BorderWidth;
}
function setBorderColor($BorderColor) {
$this->TT_BORDERCOLOR = $BorderColor;
}
function setDelay($Delay) {
$this->TT_DELAY = $Delay;
}
function setFontColor($FontColor) {
$this->TT_FONTCOLOR = $FontColor;
}
function setFontFace($FontFace) {
$this->TT_FONTFACE = $FontFace;
}
function setFontSize($FontSize) {
$this->TT_FONTSIZE = $FontSize;
}
function setFontWeight($FontWeight) {
$this->TT_FONTWEIGHT = $FontWeight;
}
function setPadding($Padding) {
$this->TT_PADDING = $Padding;
}
function setStatic($Static) {
$this->TT_STATIC = $Static;
}
function setCSSClass($Class) {
$this->CSSClass = $Class;
}
function setTarget($Target) {
$this->Target = $Target;
}
function GenerateMouseOverString($ToolTipText) {
return 'onMouseOver="'
. "this.T_BGCOLOR='" . $this->TT_BGCOLOR . "'; "
. "this.T_BORDERWIDTH='" . $this->TT_BORDERWIDTH . "'; "
. "this.T_BORDERCOLOR='" . $this->TT_BORDERCOLOR . "'; "
. "this.T_DELAY='" . $this->TT_DELAY . "'; "
. "this.T_FONTCOLOR='" . $this->TT_FONTCOLOR . "'; "
. "this.T_FONTFACE='" . $this->TT_FONTFACE . "'; "
. "this.T_FONTSIZE='" . $this->TT_FONTSIZE . "'; "
. "this.T_FONTWEIGHT='" . $this->TT_FONTWEIGHT . "'; "
. "this.T_PADDING='" . $this->TT_PADDING . "'; "
. "this.T_STATIC='" . $this->TT_STATIC . "'; "
. 'return escape(\'' . $ToolTipText . '\');">';
}
function CreateTextToolTip($PageText, $ToolTipText, $CSSClass) {
$ToolTip = '<span class="' . $CSSClass . '" '
. $this->GenerateMouseOverString($ToolTipText)
. $PageText . '</span>';
$this->ToolTip = $ToolTip;
}
function CreateLinkToolTip($Link, $CSSClass, $Target, $ToolTipText, $PageText) {
$ToolTip = '<a href="' . $Link . '" class="' . $Class . '" target="' . $Target . '" onMouseOver="'
. $this->GenerateMouseOverString($ToolTipText)
. $PageText . '</a>';
$this->ToolTip = $ToolTip;
}
function DisplayToolTip() {
echo $this->ToolTip;
}
}