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

  • Home
  • SEARCH
  • 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 9013465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:16:28+00:00 2026-06-16T03:16:28+00:00

I’m creating a large PHP project and I’ve a trivial doubt about how to

  • 0

I’m creating a large PHP project and I’ve a trivial doubt about how to proceed.

Assume we got a class books, in this class I’ve the method ReturnInfo:

function ReturnInfo($id) {
    if( is_numeric($id) ) {
        $query = "SELECT * FROM books WHERE id='" . $id . "' LIMIT 1;";

        if( $row = $this->DBDrive->ExecuteQuery($query, $FetchResults=TRUE) )   {  
                return $row;
        } else {
            return FALSE;
        }
    } else {
        throw new Exception('Books - ReturnInfo - id not valid.');          
    }
}

Then i have another method PrintInfo

function PrintInfo($id) {
    print_r( $this->ReturnInfo($id) );
}

Obviously the code sample are just for example and not actual production code.

In the second method should I check (again) if id is numeric ? Or can I skip it because is already taken care in the first method and if it’s not an exception will be thrown?

Till now I always wrote code with redundant checks (no matter if already checked elsewhere i’ll check it also here)

Is there a best practice? Is just common sense?

Thank you in advance for your kind replies.

  • 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-06-16T03:16:30+00:00Added an answer on June 16, 2026 at 3:16 am

    Well, ask yourself what you gain by checking in every layer. Is it more security? No, since the function which uses the value for something, which is the only one being vulnerable, does the checking itself.

    The only advantage it has is that you can stop invalid values earlier, which executes less code. It doesn’t have to go all the way down and back up before you know the value is invalid. This may or may not be a real advantage.

    It does create problems though: you have more code. Your code is not DRY anymore. If you change the definition of what makes a “valid” value, you have to change the checks all over the place. Those are much bigger problems.

    I’d approach the problem this way: your core business model does the in-detail checking, it is ultimately responsible for making sure the value is valid, and it is the only one doing something “dangerous” with this value. The outer layers (controllers, views) merely pass the value along. With one exception: they may do “rough” data validation. Say your model expects a phone number with specific formatting. You should check this specific rule inside the model. In the view/controller layer you may roughly validate the value as being at least somewhat numeric though. Say, you have a Javascript check. This blocks obviously wrong values from bothering your core app, while still giving you the flexibility to tweak the core validation rule in just one place.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

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.