to pass, or not to pass, that is the question

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

to pass, or not to pass, that is the question

Post by John Cartwright »

Well I've been working on some small app's to familiarize myself with the Zend Framework, and when using their filter system I seem to have come to a design question which I could use some input on.

Well, when designing some modules there is a possibility for them to default to a value, or set a value.

For instance, I'll have a view which may or may not have an id to be passed to it. In the Zend Framework, their filters will return false if the value is not valid.

Code: Select all

if (($newsId = $filtered->testDigits('newsid')) == false) 
{	
	//invalid paramater
}
						
$this->view->paintNewsArticle($newsId);
$this->view->paintNewsComments($newsId);
What I want to know is, should I

- Set the default value when I know its an invalid paramter (indicated by the comment)

- Keep the false value, and pass the variable

- Pass nothing to the view and let it check the simple logic

Not really a big deal, but I am wondering if anyone has any thought on this?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Well, if I was really really OOP I'd throw an InvalidID exception and have a higher up caller capture it and output an appropriate error message. It's the classic error bubbling problem.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Lets say I had to pass a starting point for a paginating class.. some user inputs letters in the url for some odd reason. I know it's invalid but I don't really care, I still need to paginate and there isn't much point on outputting an error message

What would I do then? Would I set the paramater to a default and pass that to the class -- or should I pass nothing to the class?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Depending on where you want to place things, the paginating class would have input handlers that would correct any "bad" values since it knows it's inner workings the best. However you could also have general handlers but then the question comes, do you want special case code to know this field is supposed to be an integer greater than zero? Personally, I feel the class should handle the actual logistics while the general handlers simple make sure the information coming in is in a uniform (unescaped) format that everything accepts as basic input data.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

This becomes a question about the dividing line between exceptions and expected invalid inputs. A simpler example relating to the one you gave is the first time you enter Paging code where not page number is given. This is not an exception, you want the thing to default to the first page of results if no valid page number is supplied. I have had a similar problem with lists where you can delete items. If you delete the last item in the list and it is alone on the last page, then returning to the previous page is now out of bounds, not unexpected, just invalid.

Where the line between gracefully responding to bad inputs and fending off hacking attacks is another aspect to this question.
(#10850)
Post Reply