Methods only purpose is print()
Moderator: General Moderators
-
timclaason
- Forum Commoner
- Posts: 77
- Joined: Tue Dec 16, 2003 9:06 am
- Location: WI
Methods only purpose is print()
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
-
timclaason
- Forum Commoner
- Posts: 77
- Joined: Tue Dec 16, 2003 9:06 am
- Location: WI
Print Methods
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:
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.
Code: Select all
//Require Class and childClass
$Class->outputHTMLStuff();
$childClass->doOtherStuff();This works for me, but I wasn't sure if that was a normal thing to do.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.
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.
(#10850)
-
timclaason
- Forum Commoner
- Posts: 77
- Joined: Tue Dec 16, 2003 9:06 am
- Location: WI
Separating HTML and PHP
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:
Better than I like this:
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.
For instance, I like this:
Code: Select all
print("<HTML><HEAD><TITLE>" . $class->getTitle() . "</TITLE></HEAD");Code: Select all
<HTML>
<HEAD>
<TITLE><?=$class->getTitle();?></TITLE>
</HEAD>- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
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.
Can you tell which will return, and which will output? 
Code: Select all
<?php
$object->dumpMessage();
$object->getMessage();
?>Due to the influence I have had from Java, I only use paint() when refering to graphical stuff.d11wtq wrote:It's actually good practise to do things like thatIt'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().