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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:01:25+00:00 2026-05-27T12:01:25+00:00

PHP 5 Type Hinting PHP 5 introduces Type Hinting. Functions are now able to

  • 0

PHP 5 Type Hinting

PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

The following excerpt from the above:

if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

Does the above mean:

if default parameters are to used use with type hinting, it can have only have NULL as the default value.

i.e. the code in code1 is wrong and results in:

Fatal error: Default value for parameters with a class type hint can only be NULL

code1:

 function setName ( string $name = "happ") {
  ...
  }

Where as code in code2 is right:

code2:

 function setName ( string $name = NULL) {
  ...
  }

Why is this constraint assigned in php?

  • 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-27T12:01:26+00:00Added an answer on May 27, 2026 at 12:01 pm

    You can’t typehint strings, you can only typehint objects and arrays, so this is incorrect:

    function setName ( string $name = "happ") {
    

    (The reason you don’t get a compile-time error here is because PHP is interpreting “string” as the name of a class.)

    The wording in the docs means that if you do this:

    function foo(Foo $arg) {
    

    Then the argument passed to foo() must be an instance of object Foo. But if you do this:

    function foo(Foo $arg = null) {
    

    Then the argument passed to foo() can either be an instance of object Foo, or null. Note also that if you do this:

    function foo(array $foo = array(1, 2, 3))
    

    Then you can’t call foo(null). If you want this functionality, you can do something like this:

    function foo(array $foo = null) {
        if ($foo === null) {
            $foo = array(1, 2, 3);
        }
    

    [Edit 1] As of PHP 5.4, you can typehint callable:

    function foo(callable $callback) {
        call_user_func($callback);
    }
    

    [Edit 2] As of PHP 7.0, you can typehint bool, float, int, and string. This makes the code in the question valid syntax. As of PHP 7.1, you can typehint iterable.

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

Sidebar

Related Questions

How can you describe the parameters and return type (getter/setter) of your PHP functions?
Since PHP has no custom-class type-casting, how would I go about doing the PHP
I want to make sure people can't type the name of a PHP script
I'm having weird problems with PHP OOP and type hinting. Here's an example: abstract
Considering the following PHP class: class someObject { public function broken(){ return isset($this->something()) ?
I can't understand the motivation of PHP authors to add the type hinting. I
I'm using type hinting on my constructor parameter list like so: public function __construct(FooRepository
I have two radio buttons <div align=center class=radio_group> <input type=radio id=gallerymenustyle1 class=element radio name=gallerymenustyle[]
Here is my code: function phpwtf(string $s) { echo $s\n; } phpwtf(Type hinting is
I want to receive the following HTTP request in PHP: Content-type: multipart/form-data;boundary=main_boundary --main_boundary Content-type:

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.