Methods only purpose is print()

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.

Moderator: General Moderators

Post Reply
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Methods only purpose is print()

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Print Methods

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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.
(#10850)
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Separating HTML and PHP

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

Post 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.
(#10850)
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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? :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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. :)
Post Reply