# type hint semantics; disagreement between phpt and manual

- Thread: https://externals.io/message/40879
- Original archive: https://news-web.php.net/php.internals/40879
- Started by Nathan Nobbe on 2008-10-07T19:59:19+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40879 — Nathan Nobbe — 2008-10-07T19:59:19+00:00

URL: https://externals.io/message/40879#40879

hi all,

we are encountering an error in our code due to type hint semantics.  php is
allowing `NULL` values through a type hint for a class, however, if i read the
manual, NULL, should only be allowed, if and only if, null is given as the
default value for the formal parameter that is type-hinted.

"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<http://ch2.php.net/manual/en/language.types.null.php>is used as
the default parameter value, it will be allowed as an argument
for any later call."

now, i have checked our code; there is no `NULL` default value, and there are
no parents which mark a default value of NULL.  so why would they be
creeping through then.. ?

well, i glanced at the phpt test, and its quite clear the tests are allowing
NULL,

Zend/tests/errmsg_013.phpt:errmsg: default value for parameters with array
type hint can only be an array or `NULL` 
Zend/tests/errmsg_013.phpt:Fatal error: Default value for parameters with
array type hint can only be an array or `NULL` in %s on line %d

looking at the function prototype in this test, `NULL` is not specefied as the
default value, so i dont think `NULL` should be allowed here anyway..
--TEST--
errmsg: default value for parameters with array type hint can only be an
array or `NULL` 
--FILE--
<?php

class test {
    function foo(array $a = "s") {
    }
}

echo "Done\n";
?>
--EXPECTF--
Fatal error: Default value for parameters with array type hint can only be
an array or `NULL` in %s on line %d

this taken from 5.2.6.RC3 source.  it seems to me, either the source or the
manual is wrong, but which one is it ??  personally, i would prefer it be
the former in this case ;)

FWIW, we are currently running php5.2.2 on centos5, but im pretty sure an
upgrade to 5.2.6 will not change the semantics, since the phpt tests were
the same in both the 5.2.2 & 5.2.6 source.

-nathan

---

## #41161 — Lukas Kahwe Smith — 2008-10-16T18:11:42+00:00

URL: https://externals.io/message/40879#41161 · In reply to: #40879

> hi all,
>
> we are encountering an error in our code due to type hint  
> semantics.  php is
> allowing `NULL` values through a type hint for a class, however, if i  
> read the
> manual, NULL, should only be allowed, if and only if, null is given  
> as the
> default value for the formal parameter that is type-hinted.
>
> "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<http://ch2.php.net/manual/en/language.types.null.php>is used as
> the default parameter value, it will be allowed as an argument
> for any later call."
>
> now, i have checked our code; there is no `NULL` default value, and  
> there are
> no parents which mark a default value of NULL.  so why would they be
> creeping through then.. ?
>
> well, i glanced at the phpt test, and its quite clear the tests are  
> allowing
> NULL,
>
> Zend/tests/errmsg_013.phpt:errmsg: default value for parameters with  
> array
> type hint can only be an array or `NULL` 
> Zend/tests/errmsg_013.phpt:Fatal error: Default value for parameters  
> with
> array type hint can only be an array or `NULL` in %s on line %d
>
> looking at the function prototype in this test, `NULL` is not  
> specefied as the
> default value, so i dont think `NULL` should be allowed here anyway..
> --TEST--
> errmsg: default value for parameters with array type hint can only  
> be an
> array or `NULL` 
> --FILE--
> <?php
>
> class test {
>    function foo(array $a = "s") {
>    }
> }
>
> echo "Done\n";
> ?>
> --EXPECTF--
> Fatal error: Default value for parameters with array type hint can  
> only be
> an array or `NULL` in %s on line %d
>
> this taken from 5.2.6.RC3 source.  it seems to me, either the source  
> or the
> manual is wrong, but which one is it ??  personally, i would prefer  
> it be
> the former in this case ;)
>
> FWIW, we are currently running php5.2.2 on centos5, but im pretty  
> sure an
> upgrade to 5.2.6 will not change the semantics, since the phpt tests  
> were
> the same in both the 5.2.2 & 5.2.6 source.


please file a bug report with a reproduceble test case that  
illustrates the conflicting behavior with the manual.

regards,
Lukas Kahwe Smith
mls@pooteeweet.org
