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 3680668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:36:12+00:00 2026-05-19T03:36:12+00:00

How do you validate/manage your PHP method arguments and why do you do it

  • 0

How do you validate/manage your PHP method arguments and why do you do it this way?

  • 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-19T03:36:13+00:00Added an answer on May 19, 2026 at 3:36 am

    Well, assuming that you’re talking about type-checking method arguments, it depends:

    1. If it’s expecting an object, I use type-hinting with an interface:

      public function foo(iBar $bar)
      
    2. If it’s expecting an array only, I use type-hinting with the array keyword.

      public function foo(array $bar)
      
    3. If it’s expecting a string, int, bool or float, I cast it:

      public function foo($bar) {
          $bar = (int) $bar;
      }
      
    4. If it’s expecting mixed, I just check in a cascade:

      public function foo($bar) {
          if (is_string($bar)) {
              //handle string case
          } elseif (is_array($bar)) {
              //...
          } else {
              throw new InvalidArgumentException("invalid type");
          }
      }
      
    5. Lastly, if it’s expecting an iterable type, I don’t use type-hinting. I check if it’s an array first, then re-load the iterator:

      public function foo($bar) {
          if (is_array($bar)) {
              $bar = new ArrayIterator($bar);
          }
          if (!$bar instanceof Traversable) {
              throw new InvalidArgumentException("Not an Iterator");
          }
      }
      
    6. If it’s expecting a filename or directory, just confirm it with is_file:

      public function foo($bar) {
          if (!is_file($bar)) {
              throw new InvalidArgumentException("File doesn't exist");
          }
      }
      

    I think that handles most of the cases. If you think of any others, I’ll gladly try to answer them…

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

Sidebar

Related Questions

Why do I get this error when I run manage.py validate ?: Traceback (most
I need to validate this simple pick list: <select name=<%= key %>> <option value=ETC
Calling Validate() on an XmlDocument requires passing in a ValidationEventHandler delegate. That event function
I need to validate a date/time field on a webpage but want it to
Is it possible to validate an xml file against its associated schema using Visual
I need to validate an XML string (and not a file) against a DTD
I am trying to validate user id's matching the example: smith.jack or smith.jack.s In
How can I validate that my ASPNET AJAX installation is correct. I have Visual
I need to validate the email address of my users. Unfortunately, making a validator
I'm using jQuery validate on a form I am building and it is working

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.