# Type hints with null default values

- Thread: https://externals.io/message/13471
- Original archive: https://news-web.php.net/php.internals/13471
- Started by Cristiano Duarte on 2004-10-22T17:41:24+00:00
- Messages: 6
- 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

---

## #13471 — Cristiano Duarte — 2004-10-22T17:41:24+00:00

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

Marcus Boerger wrote:

> Hello Robert,
> 
> Tuesday, October 19, 2004, 10:20:59 PM, you wrote:
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> As far as we disallow this and only allow optional types at the end i'd
> agre to this. Example:
> 
> public function Compare($cmpFunc, [BaseClass] $objA);
> 
I don't see any danger if we allow null with this syntax:

 public function Compare([BaseClass] $objA, $cmpFunc);

if we don't allow null with this syntax:

 public function Compare(BaseClass $objA, $cmpFunc);

and if we make an argument optional with this syntax:

 public function Compare($cmpFunc, BaseClass $objA=null);

If someone (like me) has thousands lines of code that uses 'null' meaning
'null reference' just need to use syntax #1.

If someone is very strict and don't even think about using null in
typehinted arguments, because null is not a value it's a type and must be
used as so, just need to use syntax #2.

If someone wants to make typehinted parameters optional, just use syntax #3.

I really don't see any problem allowing this three syntaxes(but I'm not very
clever since I'm for syntax #1) :-P

Regards,

Cristiano Duarte

---

## #13549 — Cristiano Duarte — 2004-10-27T00:21:03+00:00

URL: https://externals.io/message/13471#13549 · In reply to: #13471

So reading all posts on this subject, the proposal would be:

1) Allow null typehints
 public function Compare([BaseClass] $objA, $cmpFunc);

 Ex: Compare(null, 'func1');

2) Don't allow null typehints
 public function Compare(BaseClass $objA, $cmpFunc);

 Ex: Compare(new BaseClass(), 'func1');

3) Optional argument (with typehint)
 public function Compare($cmpFunc, BaseClass $objA=null);

 Ex: Compare('func1');

Am I right ?  Is it possible to implement all three syntaxes ?
IMH it would satisfy everybody's needs.

Regards,

Cristiano Duarte

---

## #13555 — Marcus Boerger — 2004-10-27T07:21:06+00:00

URL: https://externals.io/message/13471#13555 · In reply to: #13471

Hello Cristiano,

Friday, October 22, 2004, 7:41:24 PM, you wrote:


> If someone (like me) has thousands lines of code that uses 'null' meaning
> 'null reference' just need to use syntax #1.

Then you are simply using your PHP tool wrong. PHP is not Java where you
need (and have) null references.

Best regards,
 Marcus                            mailto:helly@php.net

---

## #13557 — Christian Schneider — 2004-10-27T08:53:33+00:00

URL: https://externals.io/message/13471#13557

Marcus Boerger wrote:
> So for now the only addition we may probably consider for 5.1 is
> adding 4: optional typehinted values that default to null and only
> null.

Please don't add another parameter syntax (especially with line-noi... 
err special characters) like [BaseClass]. This would make PHP more 
unreadable than it needs to be for a minor feature. To allow 
foo(BaseClass $objA = null) to also accept foo(null); handles the few 
cases where you want to pass null nicely IMHO.

Advocating to only change the PHP syntax to simplify it,
- Chris

---

## #13558 — Sebastian Bergmann — 2004-10-27T09:36:25+00:00

URL: https://externals.io/message/13471#13558

Marcus Boerger wrote:
> So for now the only addition we may probably consider for 5.1 is
> adding 4: optional typehinted values that default to null and only
> null.

 That sounds like the most sane way to do it.

-- 
Sebastian Bergmann                      http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69

---

## #13613 — Curt Zirzow — 2004-10-29T23:29:01+00:00

URL: https://externals.io/message/13471#13613 · In reply to: #13557

* Thus wrote Christian Schneider:
> Please don't add another parameter syntax (especially with line-noi... 
> err special characters) like [BaseClass]. This would make PHP more 
> unreadable than it needs to be for a minor feature. To allow 
> foo(BaseClass $objA = null) to also accept foo(null); handles the few 
> cases where you want to pass null nicely IMHO.
 
I'd have to agree, that changing the syntax on how to pass optional
class type hints, is probably not a good thing to do.  It has been
traditional to set a parameter to default by assigning a value to
the paramater, and the rule always has been what kind of value can
be assigned.

 
Curt
-- 
Quoth the Raven, "Nevermore."
