# typehinting and NULL

- Thread: https://externals.io/message/9450
- Original archive: https://news-web.php.net/php.internals/9450
- Started by Marcus Boerger on 2004-04-21T10:03:21+00:00
- Messages: 4
- 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

---

## #9450 — Marcus Boerger — 2004-04-21T10:03:21+00:00

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

Hello Andi, Zeev, internals,

  after readin the PHP articles on PHP5 and the rerequested typehinting
for arrays i once again thought about typehints and NULL.

At the moment typehints allow `NULL` always. That is complete nonsense
because `NULL` doesn't pass any instance-of check. And in general there
is only one operation that leagally returns true when working with `NULL` 
values. That is `NULL` === NULL. In php `NULL` == `NULL` simply returns true
because `NULL` equals zero. Hence:
        $ php -r 'var_dump(NULL == NULL);'
        bool(true)
        $ php -r 'var_dump(NULL == 0);'
        bool(true)
        $ php -r 'var_dump(NULL === NULL);'
        bool(true)
AND:
    $ php -r '$o=new stdClass; var_dump($o instanceof stdClass);'
    bool(true)
    $ php -r 'var_dump(NULL instanceof stdClass);'
    bool(false)

At least we must find a way to decide whether or not `NULL` should be allowed
NULL. Yes enforcing NOL NULL would make too many people complain they cannot
pass a `NULL` by ref to have it returned as an instance of the class - though
reconsider your code should be the answer here. Looking up the discussions
about the issue we already had a solution for this from Zeev.

function allowingNULL(object $val = NULL)
function preventNULL(object $val)

any thoughts?

Just to stress out: The current situation doesn't respect generally accepted
inheritance rules.

Best regards,
 Marcus

---

## #9452 — Christian Schneider — 2004-04-21T10:15:01+00:00

URL: https://externals.io/message/9450#9452 · In reply to: #9450

Marcus Boerger wrote:
> function allowingNULL(object $val = NULL)
> function preventNULL(object $val)

I like this idea even though it's abusing the default value syntax.

What's a bit weird is something like
function foo(object $obj = NULL, $mandatoryparameter) { ... }
where people will wonder why there is a default value even though they 
can't omit the parameter really. But personally I could live with that.

- Chris

---

## #9454 — Andi Gutmans — 2004-04-21T10:25:15+00:00

URL: https://externals.io/message/9450#9454 · In reply to: #9450

I wanted to support:
function preventNULL($val not NULL)
function allowNULL($val)

However, I left it for after 5.0.0 due to the feature freeze. Note, that it 
will also not change today's semantics.

Andi

At 12:15 PM 4/21/2004 +0200, Christian Schneider wrote:
>Marcus Boerger wrote:
> > […]
>
>I like this idea even though it's abusing the default value syntax.
>
>What's a bit weird is something like
>function foo(object $obj = NULL, $mandatoryparameter) { ... }
>where people will wonder why there is a default value even though they 
>can't omit the parameter really. But personally I could live with that.
>
>- Chris

---

## #9472 — Marcus Boerger — 2004-04-21T23:21:24+00:00

URL: https://externals.io/message/9450#9472 · In reply to: #9454

Hello Andi,

sounds like a great idea, maybe even the best one.

marcus

Wednesday, April 21, 2004, 12:25:15 PM, you wrote:

> I wanted to support:
> function preventNULL($val not NULL)
> function allowNULL($val)

> However, I left it for after 5.0.0 due to the feature freeze. Note, that it
> will also not change today's semantics.

> Andi

> At 12:15 PM 4/21/2004 +0200, Christian Schneider wrote:
> > […]




-- 
Best regards,
 Marcus                            mailto:helly@php.net
