Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
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.
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?
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.
"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."
"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."
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.
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.
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.
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
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.
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?