Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3856118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:52:50+00:00 2026-05-19T17:52:50+00:00

How should I handle the input validation of a method? Of these two, which

  • 0

How should I handle the input validation of a method?
Of these two, which one is the more correct way? or there is a better way
This method is called by the constructor and $prodID can be user input or come from the db.

private function fill_data1($prodID)
{
    //Way 1
    filter_var($prodID, FILTER_VALIDATE_INT, array('options'=>array('min_range'=>1, 'max_range'=>1000000)));
    if (is_null($prodID)) {
        return FALSE;
    } elseif ($prodID === FALSE) {
        return FALSE;
    }
    $prod = getArtData($prodID);
    $this->set_id($prod['artID']);
    $this->set_name($prod['artName']);
    $this->set_price($prod['precio']);
}

private function fill_data(2$prodID)
{
    //Way 2
    filter_var($prodID, FILTER_VALIDATE_INT, array('options'=>array('min_range'=>1, 'max_range'=>1000000)));
    if (is_null($prodID) || $prodID === FALSE)
    {
        die('invalid input for prodID (' . $prodID . '). It has to be an integer > 0');
    }
    $prod = getArtData($prodID);
    $this->set_id($prod['artID']);
    $this->set_name($prod['artName']);
    $this->set_price($prod['precio']);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T17:52:51+00:00Added an answer on May 19, 2026 at 5:52 pm

    Option 3: Use exceptions and put the ID validation as close to the data as possible.

    public function getArtData($id) {
        if (!is_int($id) || $id <= 0) {
            throw new InvalidIdentifierException(
                    "Article ID $id must be a valid, positive integer.");
        }
        ...
    }
    

    The problem with returning false is that you must check for the return value to handle and skip it. If where you handle it (the presentation layer) is several function calls removed from where it’s validated (the data layer), you have to check for false at each level. An exception will propagate up to the first function that catches it, bypassing the remaining code in each function along the way.

    function displayProductAction(...) {
        $prodID = $request->getParam('prod');
        $form = ...
        try {
            $form->fill_data($prodID)
            $view->form = $form;
        }
        catch (InvalidIdentifierException $e) {
            $view->error = $e->getMessage();
        }
        $view->render();
    }
    

    Calling die() causes its own difficulties. It is harder to unit test and it forces you to put the error-display code at the point of failure. What happens when you want to use your code from a web-service?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is using view in db design right method or we should handle it code
What is the most secure way to handle forgotten passwords/password resets? Should I email
There is a small program which takes input from users on a prompt. It
I have a service which should handle documents by downloading and uploading to it.
I have googled a bit for how I should handle security in a web
I'm trying to figure out how my new app should handle data. In previous
How should I handle image uploading using PHP? How should I handle the chmod
I have thread exception handler which saves the exception stack trace and should close
What special method(s?) should I redefine in my class so that it handled AttributeError
According to http://www.w3.org/TR/html401/interact/forms.html#h-17.4 , an input element should end with a single > and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.