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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:37:21+00:00 2026-06-03T01:37:21+00:00

Is there a way to require a function parameter is a specific datatype such

  • 0

Is there a way to require a function parameter is a specific datatype such as an array? For instance something like this:

function example(array $array){}

If so does that work for all datatypes? Is there a good resource that would show me how to do this?

  • 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-03T01:37:22+00:00Added an answer on June 3, 2026 at 1:37 am

    Edit: Yes, you can type-hint with arrays, so edited my answer and changed accordingly.

    What you want to do is called type-hinting. You can’t type hint basic data types, such as int, string, bool. You can type-hint with array or objects and interfaces:

    function example_hinted1(array $arr) {
    
    }
    
    function example_hinted2(User $user) {
    
    }
    

    Calling example_hinted1(5) will generate a PHP fatal error (not an exception), but calling it passing an array is totally ok.

    If you need to be sure that some argument to a function is from a basic type you can simulate this behavior with code inside your function:

    function example($number) {
      if (!is_int($number) {
        throw new Exception("You must pass an integer to ".__FUNCTION__."()");
      }
      // rest of your function
    }
    

    So, these snippets would work:

    example(1);
    
    $b = 5 + 8;
    example($b);
    

    while these would throw an exception:

    example('foo');
    example(array(5, 6));
    example(new User());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this: case class FunctionCommand[A](function: Function1[Array[A], Unit]) class MyClass(commands: List[FunctionCommand[_]])
Is there a way to require that a class have a particular abstract member?
Is there a way to require an API key in the URL / or
Is there any way to include php file using require and calling a php
I have a asp.net mvc application. Some pages require SSL, is there a way
is there any other simple,nicer way? require 'pp' a1 = [02/28/10,Webinars,131,0,26 Feb 2010,0,3d, 8h,
is there way how to get name ov event from Lambda expression like with
Is there an easy way to set a callback function to a new window
I simply want to know if there is a way to mock an array
Is there a way to make this work (Scala 2.8.1): class A class B

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.