Page 1 of 1
Methods only purpose is print()
Posted: Tue Dec 19, 2006 1:49 pm
by timclaason
Is it bad style to have a method (for instance, an error generating method called errorMessage()) where all it does is print()? In other words, it doesn't return a value. All it does is outputs a message.
Posted: Tue Dec 19, 2006 2:02 pm
by RobertGonzalez
I wouldn't say it is bad. If that is the necessity of the app, then it belongs. At some point something needs to get to the screen, right?
Posted: Tue Dec 19, 2006 2:04 pm
by neophyte
I think that would depend on the usage. Lets say the function was an error output/storage device. Wrapping print in a function might be a good way to ensure future revisions to how errors display would be a quick change instead of a laborious. Imagine changing 100's of direct print error message to a function or object.
Print Methods
Posted: Tue Dec 19, 2006 2:09 pm
by timclaason
True. I'm a pretty simple designer, though. I try to be as consistent as possible, and generally try to make pages in my web apps like this:
Code: Select all
//Require Class and childClass
$Class->outputHTMLStuff();
$childClass->doOtherStuff();
This way, I put all the HTML code in methods, and I minimize the amount of code/text/html that is actually in the pages (making the bulk of output more centralized), then my parent classes do HTML output stuff, and my child classes do programming stuff.
This works for me, but I wasn't sure if that was a normal thing to do.
Posted: Tue Dec 19, 2006 2:40 pm
by Chris Corbyn
It's actually good practise to do things like that

It's called abstraction and having it there provides more points of opportunity to change the behaviour of your code. Often a method which prints something would be called paint().
Posted: Tue Dec 19, 2006 4:11 pm
by Christopher
Although it is probably a better direction to separate your HTML and PHP if possible -- mainly because it clarifies the separation and dependencies between layers and concerns. It's not that you use echo(), it is how you use echo() that matters. I believe the current practice is to gather your response into one object and have that echo. Debugging and Logging are obviously separate from that.
You question is a little like asking "is it bad to use switch statements?" Well ... not occasionally as appropriate, but if that's all you use and they are all over your code then something is amiss.
Separating HTML and PHP
Posted: Tue Dec 19, 2006 4:27 pm
by timclaason
I've definitely considered the concept of separating HTML and PHP, and I just find that I like using methods to output HTML, rather than statically putting it in.
For instance, I like this:
Code: Select all
print("<HTML><HEAD><TITLE>" . $class->getTitle() . "</TITLE></HEAD");
Better than I like this:
Code: Select all
<HTML>
<HEAD>
<TITLE><?=$class->getTitle();?></TITLE>
</HEAD>
I used to use server side includes to just have it output HTML without having to clutter up the page, but as I've begun to use OOP more in my code, I like to have everything happening in a page be a call to some method I have in my central class or child class.
Posted: Tue Dec 19, 2006 4:38 pm
by Christopher
What you are doing looks good to me. Sorry by HTML and PHP I really meant you presentation from the rest of your code (flow and domain). Either of the above do that. But the question is then how are you packaging the code above? I should be in a View object.
Posted: Wed Dec 20, 2006 4:26 am
by raghavan20
If I am not wrong, then it can be considered V of MVC model as the function prints what the controller C wants to.
Posted: Wed Dec 20, 2006 1:54 pm
by Jenk
So far, the only instances I have had a method output directly is in an exception/error handling class I use, where the templates/views may not be available - and like you, I name the method with an indication that it will output directly, rather than return.
Code: Select all
<?php
$object->dumpMessage();
$object->getMessage();
?>
Can you tell which will return, and which will output?

Posted: Wed Dec 20, 2006 1:55 pm
by Jenk
d11wtq wrote:It's actually good practise to do things like that

It's called abstraction and having it there provides more points of opportunity to change the behaviour of your code. Often a method which prints something would be called paint().
Due to the influence I have had from Java, I only use paint() when refering to graphical stuff.
