# Return Type Hinting for Methods RFC

- Thread: https://externals.io/message/56974
- Original archive: https://news-web.php.net/php.internals/56974
- Started by Will Fitch on 2011-12-21T02:09:46+00:00
- Messages: 76
- Page 1 of 4: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/56974.md?page=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

---

## #56974 — Will Fitch — 2011-12-21T02:09:46+00:00

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

Hello All,

I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion.  A link to the patch for this is provided and can be ran against the current HEAD.  

There is an older entry still in existence, but this patch is syntactically different.  The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.

If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?

-- Will

---

## #56975 — Christopher Jones — 2011-12-21T03:05:22+00:00

URL: https://externals.io/message/56974#56975 · In reply to: #56974

> Hello All,
>
> I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion.  A link to the patch for this is provided and can be ran against the current HEAD.
>
> There is an older entry still in existence, but this patch is syntactically different.  The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
>
> If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
>
> -- Will

I've added it to https://wiki.php.net/rfc/typechecking with the other related RFCs.
This is linked from the "Under Discussion" section.

Chris

-- 
Email: christopher.jones@oracle.com
Tel:  +1 650 506 8630
Blog:  http://blogs.oracle.com/opal/

---

## #56984 — Nikita Popov — 2011-12-21T17:17:31+00:00

URL: https://externals.io/message/56974#56984 · In reply to: #56974

Hi Will!

One random thought I had while reading the RFC is: What about the
newly introduced callable typehint? Is this missing by intention? I
could well imagine so (because it's hard to check what scope
callability should be checked on), but wanted to be sure on that.

Nikita


> Hello All,
>
> I would like to submit https://wiki.php.net/rfc/returntypehint2 into discussion.  A link to the patch for this is provided and can be ran against the current HEAD.
>
> There is an older entry still in existence, but this patch is syntactically different.  The older entry is located at https://wiki.php.net/rfc/typechecking and is bundled with parameter, scalars, etc.
>
> If possible, can someone promote this to the "Under Discussion" category within https://wiki.php.net/rfc?
>
> -- Will

---

## #56985 — Will Fitch — 2011-12-21T17:22:10+00:00

URL: https://externals.io/message/56974#56985 · In reply to: #56984

Hi Nikita,

I didn't add that as it's not yet in production.  As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint.  The change wouldn't be different from parameter type hinting, and can easily be added.



> Hi Will!
> 
> One random thought I had while reading the RFC is: What about the
> newly introduced callable typehint? Is this missing by intention? I
> could well imagine so (because it's hard to check what scope
> callability should be checked on), but wanted to be sure on that.
> 
> Nikita
> 
> 
> > […]

---

## #56989 — Pierre Joye — 2011-12-21T19:33:43+00:00

URL: https://externals.io/message/56974#56989 · In reply to: #56985

hi Will,

You should add it now, while 5.4 final is not released yet, this
feature exists already and should be part of the RFC, to be complete.

Cheers,


> Hi Nikita,
>
> I didn't add that as it's not yet in production.  As soon as things are finalized and 5.4 is GA, I will gladly add the callable type hint.  The change wouldn't be different from parameter type hinting, and can easily be added.
>
> 
>
> > […]
>
>
> --
> 
> 
>



-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #56990 — Will Fitch — 2011-12-21T19:36:04+00:00

URL: https://externals.io/message/56974#56990 · In reply to: #56989

Will add tonight and generate a new patch.  




> hi Will,
> 
> You should add it now, while 5.4 final is not released yet, this
> feature exists already and should be part of the RFC, to be complete.
> 
> Cheers,
> 
> 
> > […]
> 
> 
> 
> -- 
> Pierre
> 
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #56992 — Will Fitch — 2011-12-21T22:28:51+00:00

URL: https://externals.io/message/56974#56992 · In reply to: #56989

Hi Pierre and Nikita,

I have added callable to the patch and updated the RFC entry to reflect the changes. Please verify and let me know if you have any issues.



> hi Will,
> 
> You should add it now, while 5.4 final is not released yet, this
> feature exists already and should be part of the RFC, to be complete.
> 
> Cheers,
> 
> 
> > […]
> 
> 
> 
> -- 
> Pierre
> 
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #56993 — Dmitri Snytkine — 2011-12-22T14:09:40+00:00

URL: https://externals.io/message/56974#56993 · In reply to: #56992

The return type hinting is probably the biggest thing happening in php in a
long time.
Is this too good to be true?

With return type hinting I can see a huge improvenets in php IDEs in
spotting errors. Also it will be much easier to auto generate wsdl files
when return types are known to a compiler.
These are just to name a couple of important benefits that can come out of
it.

What I don't see is any discussion of being able to declare the type of
variable, you now, like in Java.
ie: 
private \ArrayObject $customers;

Also, is there an implicit "void" return type?

I hope that declaring types of variables will be added too because without
it, this feature is not all that useful. 

Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: Will Fitch [mailto:will.fitch@gmail.com] 
Sent: Wednesday, December 21, 2011 5:29 PM
To: Pierre Joye
Cc: Nikita Popov; PHP Developers Mailing List
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC

Hi Pierre and Nikita,

I have added callable to the patch and updated the RFC entry to reflect the
changes. Please verify and let me know if you have any issues.



> hi Will,
> 
> You should add it now, while 5.4 final is not released yet, this
> feature exists already and should be part of the RFC, to be complete.
> 
> Cheers,
> 
> 
> > […]
finalized and 5.4 is GA, I will gladly add the callable type hint.  The
change wouldn't be different from parameter type hinting, and can easily be
added.
> > […]
wrote:
> > […]
discussion.  A link to the patch for this is provided and can be ran against
the current HEAD.
> > […]
syntactically different.  The older entry is located at
https://wiki.php.net/rfc/typechecking and is bundled with parameter,
scalars, etc.
> > […]
category within https://wiki.php.net/rfc?
> > […]
> 
> 
> 
> -- 
> Pierre
> 
> @pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #56994 — Sebastian Krebs — 2011-12-22T14:33:40+00:00

URL: https://externals.io/message/56974#56994 · In reply to: #56993

2011/12/22 Dmitri Snytkine <dsnytkine@ultralogistics.com>

> The return type hinting is probably the biggest thing happening in php in a
> long time.
> Is this too good to be true?
>
> With return type hinting I can see a huge improvenets in php IDEs in
> spotting errors. Also it will be much easier to auto generate wsdl files
> when return types are known to a compiler.
> These are just to name a couple of important benefits that can come out of
> it.
>
> What I don't see is any discussion of being able to declare the type of
> variable, you now, like in Java.
> ie:
> private \ArrayObject $customers;
>

Once more "Why is PHP not Java?"


>
> Also, is there an implicit "void" return type?
>
> I hope that declaring types of variables will be added too because without
> it, this feature is not all that useful.
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
> -----Original Message-----
> From: Will Fitch [mailto:will.fitch@gmail.com]
> Sent: Wednesday, December 21, 2011 5:29 PM
> To: Pierre Joye
> Cc: Nikita Popov; PHP Developers Mailing List
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
>
> Hi Pierre and Nikita,
>
> I have added callable to the patch and updated the RFC entry to reflect the
> changes. Please verify and let me know if you have any issues.
>
> 
>
> > […]
> wrote:
> > […]
> finalized and 5.4 is GA, I will gladly add the callable type hint.  The
> change wouldn't be different from parameter type hinting, and can easily be
> added.
> > […]
> wrote:
> > […]
> discussion.  A link to the patch for this is provided and can be ran
> against
> the current HEAD.
> > […]
> syntactically different.  The older entry is located at
> https://wiki.php.net/rfc/typechecking and is bundled with parameter,
> scalars, etc.
> > […]
> category within https://wiki.php.net/rfc?
> > […]

---

## #56995 — Alain Williams — 2011-12-22T14:47:01+00:00

URL: https://externals.io/message/56974#56995 · In reply to: #56993

> The return type hinting is probably the biggest thing happening in php in a
> long time.
> Is this too good to be true?
> 
> With return type hinting I can see a huge improvenets in php IDEs in
> spotting errors. Also it will be much easier to auto generate wsdl files
> when return types are known to a compiler.
> These are just to name a couple of important benefits that can come out of
> it.
> 
> What I don't see is any discussion of being able to declare the type of
> variable, you now, like in Java.
> ie: 
> private \ArrayObject $customers;

That looks like a namespace - I don't like that syntax.

> Also, is there an implicit "void" return type?
> 
> I hope that declaring types of variables will be added too because without
> it, this feature is not all that useful. 

If - we do allow variable declaration to specify a type, can we also have a
syntax that simply says ''this variable is declared but I am not saying what type it is''.
Ie the variable can take any type - just as now.

Why ? One thing that I would really like to see in PHP is the ability to force
variables in a module to be declared -- AKA perl's 'use strict'. Have the compiler
complain if it sees a variable that has not been declared.
In a large program the occasional typeo on variable names does happen, catching them
with 'use strict' is great!

I have proposed this before and people did not like it. Here is to hopeing for a
different sentiment.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #56996 — Will Fitch — 2011-12-22T14:48:54+00:00

URL: https://externals.io/message/56974#56996 · In reply to: #56993

> The return type hinting is probably the biggest thing happening in php in a
> long time.
> Is this too good to be true?

Hopefully not.  We will see when the voting commences. :)

> 
> With return type hinting I can see a huge improvenets in php IDEs in
> spotting errors. Also it will be much easier to auto generate wsdl files
> when return types are known to a compiler.
> These are just to name a couple of important benefits that can come out of
> it.
> 
> What I don't see is any discussion of being able to declare the type of
> variable, you now, like in Java.
> ie: 
> private \ArrayObject $customers;

Type hinting is relatively new to PHP, so we need to take careful steps in moving forward.  With the introduction of parameter type hinting 5.0, and return type hinting following its lead, we will set ourselves up for a good base for a type hinting API and structure.  That said, PHP is not Java, and type hinting in PHP is very different.  It is, in fact, type *hinting*.  This is a runtime feature.  Only interface validation is verified at compile time, so we have to consider performance vs. feature in these cases.

> 
> Also, is there an implicit "void" return type?

The following types are allowed for both parameters and method returns:

- Callable
- Object (class name)
- Array

By not declaring a type hint with a parameter, it is considered mixed.  With method returns, simply using the keyword "function" as you have been will signify mixed as well.

> 
> I hope that declaring types of variables will be added too because without
> it, this feature is not all that useful. 
> 
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
> 
> "A Top 100 Logistics I.T. Provider in 2011"
> 
> 
> -----Original Message-----
> From: Will Fitch [mailto:will.fitch@gmail.com] 
> Sent: Wednesday, December 21, 2011 5:29 PM
> To: Pierre Joye
> Cc: Nikita Popov; PHP Developers Mailing List
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
> 
> Hi Pierre and Nikita,
> 
> I have added callable to the patch and updated the RFC entry to reflect the
> changes. Please verify and let me know if you have any issues.
> 
> 
> 
> > […]
> finalized and 5.4 is GA, I will gladly add the callable type hint.  The
> change wouldn't be different from parameter type hinting, and can easily be
> added.
> > […]
> wrote:
> > […]
> discussion.  A link to the patch for this is provided and can be ran against
> the current HEAD.
> > […]
> syntactically different.  The older entry is located at
> https://wiki.php.net/rfc/typechecking and is bundled with parameter,
> scalars, etc.
> > […]
> category within https://wiki.php.net/rfc?
> > […]

---

## #56997 — Alain Williams — 2011-12-22T14:51:59+00:00

URL: https://externals.io/message/56974#56997 · In reply to: #56994

> > […]
> 
> Once more "Why is PHP not Java?"

That comment confuses matters. People use PHP for all sorts of reasons, the
desire to tighten up in some places, on some occasions, should not be treated
with derision.

Tighter declarations, or any declarations at all, would not be mandatory. It would
be something that some projects might want to do. It would be nice if this could be
done on a module but module basis ... eg the implementors of a class library might
want to be really strict while allowing the class users to not be strict.

Just because Java has a feature that you appear to not like, does not mean that
others might not want it in some circumstances.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #56998 — Dmitri Snytkine — 2011-12-22T14:59:16+00:00

URL: https://externals.io/message/56974#56998

I never said to make it mandatory, only to make it available. Just like the
type hinting of function call is optional but not mandatory, which is a big
advantage over Java in my opinion. 
After all, if you make return type hinting mandatory, it would break 100% of
all existing php scripts.

To clarify I am asking to make type hinting for variable declaration an
available option. 

Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: Alain Williams [mailto:addw@phcomp.co.uk] 
Sent: Thursday, December 22, 2011 9:52 AM
To: internals@lists.php.net
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC



> > […]
> 
> Once more "Why is PHP not Java?"

That comment confuses matters. People use PHP for all sorts of reasons, the
desire to tighten up in some places, on some occasions, should not be
treated
with derision.

Tighter declarations, or any declarations at all, would not be mandatory. It
would
be something that some projects might want to do. It would be nice if this
could be
done on a module but module basis ... eg the implementors of a class library
might
want to be really strict while allowing the class users to not be strict.

Just because Java has a feature that you appear to not like, does not mean
that
others might not want it in some circumstances.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information:
http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #56999 — Nikita Popov — 2011-12-22T15:02:27+00:00

URL: https://externals.io/message/56974#56999 · In reply to: #56998

Could we maybe keep this discussion contained to return value type
hints please? Optional variable type hinting is a completely different
topic that would require a separate RFC and implementation. If you are
interested in this you can propose a RFC for this.

Nikita

On Thu, Dec 22, 2011 at 3:59 PM, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> I never said to make it mandatory, only to make it available. Just like the
> type hinting of function call is optional but not mandatory, which is a big
> advantage over Java in my opinion.
> After all, if you make return type hinting mandatory, it would break 100% of
> all existing php scripts.
>
> To clarify I am asking to make type hinting for variable declaration an
> available option.
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
> -----Original Message-----
> From: Alain Williams [mailto:addw@phcomp.co.uk]
> Sent: Thursday, December 22, 2011 9:52 AM
> To: internals@lists.php.net
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
>
> 
>
> > […]
>
> That comment confuses matters. People use PHP for all sorts of reasons, the
> desire to tighten up in some places, on some occasions, should not be
> treated
> with derision.
>
> Tighter declarations, or any declarations at all, would not be mandatory. It
> would
> be something that some projects might want to do. It would be nice if this
> could be
> done on a module but module basis ... eg the implementors of a class library
> might
> want to be really strict while allowing the class users to not be strict.
>
> Just because Java has a feature that you appear to not like, does not mean
> that
> others might not want it in some circumstances.
>
> --
> Alain Williams
> Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
> Lecturer.
> +44 (0) 787 668 0256  http://www.phcomp.co.uk/
> Parliament Hill Computers Ltd. Registration Information:
> http://www.phcomp.co.uk/contact.php
> #include <std_disclaimer.h

---

## #57000 — Keloran — 2011-12-22T15:08:16+00:00

URL: https://externals.io/message/56974#57000 · In reply to: #56998

i would love to see this expanded aswell (the way type hinting on function
variables was supposed to be), so that it could be

string, int

e.g.
function int test(bool $tester) {
 if ($tester) { return 5; }
 return 99;
}

On Thu, Dec 22, 2011 at 2:59 PM, Dmitri Snytkine <
dsnytkine@ultralogistics.com> wrote:

> I never said to make it mandatory, only to make it available. Just like the
> type hinting of function call is optional but not mandatory, which is a big
> advantage over Java in my opinion.
> After all, if you make return type hinting mandatory, it would break 100%
> of
> all existing php scripts.
>
> To clarify I am asking to make type hinting for variable declaration an
> available option.
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
> -----Original Message-----
> From: Alain Williams [mailto:addw@phcomp.co.uk]
> Sent: Thursday, December 22, 2011 9:52 AM
> To: internals@lists.php.net
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
>
> 
>
> > […]
>
> That comment confuses matters. People use PHP for all sorts of reasons, the
> desire to tighten up in some places, on some occasions, should not be
> treated
> with derision.
>
> Tighter declarations, or any declarations at all, would not be mandatory.
> It
> would
> be something that some projects might want to do. It would be nice if this
> could be
> done on a module but module basis ... eg the implementors of a class
> library
> might
> want to be really strict while allowing the class users to not be strict.
>
> Just because Java has a feature that you appear to not like, does not mean
> that
> others might not want it in some circumstances.
>
> --
> Alain Williams
> Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
> Lecturer.
> +44 (0) 787 668 0256  http://www.phcomp.co.uk/
> Parliament Hill Computers Ltd. Registration Information:
> http://www.phcomp.co.uk/contact.php
> #include <std_disclaimer.h

---

## #57001 — Dmitri Snytkine — 2011-12-22T15:11:45+00:00

URL: https://externals.io/message/56974#57001

I think the return type hinting really depends on variable type hinting. 
A simple example whould bea typical getter function

public function \Customer getCustomer(){
  return $this->customer;
}

If the $customer instance variable is not declared with the type Customer
then first of all IDE will not be able to spot an error, second compiler may
have a slighly harder time of detecting a mismatch.

I really believe that variable type hinting should be worked on at the same
time or even before the type hinting for returns.

But it's not up to me of cause, just adding my thoughts to the discussion of
this topic. I think this is a very important topic by the way.

Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: Nikita Popov [mailto:nikita.ppv@googlemail.com] 
Sent: Thursday, December 22, 2011 10:02 AM
To: Dmitri Snytkine
Cc: Alain Williams; internals@lists.php.net
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC

Could we maybe keep this discussion contained to return value type
hints please? Optional variable type hinting is a completely different
topic that would require a separate RFC and implementation. If you are
interested in this you can propose a RFC for this.

Nikita

On Thu, Dec 22, 2011 at 3:59 PM, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> I never said to make it mandatory, only to make it available. Just like
the
> type hinting of function call is optional but not mandatory, which is a
big
> advantage over Java in my opinion.
> After all, if you make return type hinting mandatory, it would break 100%
of
> all existing php scripts.
>
> To clarify I am asking to make type hinting for variable declaration an
> available option.
>
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
>
> "A Top 100 Logistics I.T. Provider in 2011"
>
>
> -----Original Message-----
> From: Alain Williams [mailto:addw@phcomp.co.uk]
> Sent: Thursday, December 22, 2011 9:52 AM
> To: internals@lists.php.net
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
>
> 
>
> > […]
>
> That comment confuses matters. People use PHP for all sorts of reasons,
the
> desire to tighten up in some places, on some occasions, should not be
> treated
> with derision.
>
> Tighter declarations, or any declarations at all, would not be mandatory.
It
> would
> be something that some projects might want to do. It would be nice if this
> could be
> done on a module but module basis ... eg the implementors of a class
library
> might
> want to be really strict while allowing the class users to not be strict.
>
> Just because Java has a feature that you appear to not like, does not mean
> that
> others might not want it in some circumstances.
>
> --
> Alain Williams
> Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
> Lecturer.
> +44 (0) 787 668 0256  http://www.phcomp.co.uk/
> Parliament Hill Computers Ltd. Registration Information:
> http://www.phcomp.co.uk/contact.php
> #include <std_disclaimer.h

---

## #57002 — Alain Williams — 2011-12-22T15:17:32+00:00

URL: https://externals.io/message/56974#57002 · In reply to: #56998

> I never said to make it mandatory, only to make it available. Just like the
> type hinting of function call is optional but not mandatory, which is a big
> advantage over Java in my opinion. 
> After all, if you make return type hinting mandatory, it would break 100% of
> all existing php scripts.

Agreed. I did not make myself clear enough: It should be optional/available but
with an option to make it mandatory for a module -- on a module by module basis so
that it did not affect code outside of the module.

> To clarify I am asking to make type hinting for variable declaration an
> available option. 

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #57003 — Ferenc Kovacs — 2011-12-22T15:17:33+00:00

URL: https://externals.io/message/56974#57003 · In reply to: #57000

scalar type hinting was rejected in the past multiple times.
I think that expanding this RFC (against the wish of the original author of
the RFC) would be a sure way to guarantee the same result for this RFC as
well..



> i would love to see this expanded aswell (the way type hinting on function
> variables was supposed to be), so that it could be
>
> string, int
>
> e.g.
> function int test(bool $tester) {
>  if ($tester) { return 5; }
>  return 99;
> }
>
> On Thu, Dec 22, 2011 at 2:59 PM, Dmitri Snytkine <
> dsnytkine@ultralogistics.com> wrote:
>
> > […]
> the
> > […]
> big
> > […]
> the
> > […]
> this
> > […]
> mean
> > […]
> IT
> > […]
>



-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #57004 — Oleg Oshmyan — 2011-12-22T15:18:14+00:00

URL: https://externals.io/message/56974#57004 · In reply to: #57001

> public function \Customer getCustomer(){
>  return $this->customer;
> }
> 
> If the $customer instance variable is not declared with the type Customer
> then first of all IDE will not be able to spot an error, second compiler may
> have a slighly harder time of detecting a mismatch.

The compiler does not even try to detect a mismatch; type hinting is
taken into account at run-time only. So whenever this function executes,
if it tries to return a value that is not a \Customer, a run-time error
will be reported.

-- 
Oleg

---

## #57005 — Will Fitch — 2011-12-22T15:29:04+00:00

URL: https://externals.io/message/56974#57005 · In reply to: #57001

> I think the return type hinting really depends on variable type hinting. 
> A simple example whould bea typical getter function
> 
> public function \Customer getCustomer(){
>  return $this->customer;
> }

The actual syntax would be: 

public \Customer getCustomer()

> 
> If the $customer instance variable is not declared with the type Customer
> then first of all IDE will not be able to spot an error, second compiler may
> have a slighly harder time of detecting a mismatch.

This isn't a compile time feature.  The validation happens at runtime.  Many IDEs actually use PHP executables to detect syntax errors only.  In terms of code flow, they'll still need to rely on Doc comments and lexical analysis.

> 
> I really believe that variable type hinting should be worked on at the same
> time or even before the type hinting for returns.
> 
> But it's not up to me of cause, just adding my thoughts to the discussion of
> this topic. I think this is a very important topic by the way.
> 
> Dmitri Snytkine
> Web Developer
> Ultra Logistics, Inc.
> Phone: (888) 220-4640 x 2097
> Fax: (888) 795-6642
> E-Mail: dsnytkine@ultralogistics.com
> Web: www.ultralogistics.com
> 
> "A Top 100 Logistics I.T. Provider in 2011"
> 
> 
> -----Original Message-----
> From: Nikita Popov [mailto:nikita.ppv@googlemail.com] 
> Sent: Thursday, December 22, 2011 10:02 AM
> To: Dmitri Snytkine
> Cc: Alain Williams; internals@lists.php.net
> Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC
> 
> Could we maybe keep this discussion contained to return value type
> hints please? Optional variable type hinting is a completely different
> topic that would require a separate RFC and implementation. If you are
> interested in this you can propose a RFC for this.
> 
> Nikita
> 
> On Thu, Dec 22, 2011 at 3:59 PM, Dmitri Snytkine
> <dsnytkine@ultralogistics.com> wrote:
> > […]
> the
> > […]
> big
> > […]
> of
> > […]
> the
> > […]
> It
> > […]
> library
> > […]

---

## #57006 — Will Fitch — 2011-12-22T15:43:23+00:00

URL: https://externals.io/message/56974#57006 · In reply to: #57004

> > […]
> 
> The compiler does not even try to detect a mismatch; type hinting is
> taken into account at run-time only. So whenever this function executes,
> if it tries to return a value that is not a \Customer, a run-time error
> will be reported.

The only exception is implementing an interface that defines a returned type hint.  This is detected at compile time but only to the point that the implemented method prototype is the same type hint.  If it is not, an `E_COMPILE_ERROR` is raised.  The runtime error raised is an E_RECOVERABLE_ERROR.

> 
> -- 
> Oleg

---

## #57007 — Stas Malyshev — 2011-12-22T18:28:13+00:00

URL: https://externals.io/message/56974#57007 · In reply to: #56999

Hi!

> Could we maybe keep this discussion contained to return value type
> hints please? Optional variable type hinting is a completely different
> topic that would require a separate RFC and implementation. If you are
> interested in this you can propose a RFC for this.

Actually, as I said many times in previous discussions on the topic, 
strict typing is useless (and, IMHO, even worse than that) if applied in 
small parts. If you have strict typing in return values, you'd have to 
have strict typing in variables, etc. - otherwise you'd have no way to 
protect yourself from hard-to-find runtime errors. And, actually, after 
that you'd have to make PHP compiled - since otherwise runtime errors 
will still be there.
So in my opinion restricting discussion to return type strict typing is 
just deluding yourself.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #57008 — Stas Malyshev — 2011-12-22T18:39:06+00:00

URL: https://externals.io/message/56974#57008 · In reply to: #56996

Hi!

> That said, PHP is not Java, and type hinting in PHP is very
> different.  It is, in fact, type *hinting*.  This is a runtime
> feature.  Only interface validation is verified at compile time, so
> we have to consider performance vs. feature in these cases.

Naming it "hinting" was probably the worst mistake in PHP
documentation since "safe mode". The word hinting implies it's just 
something non-mandatory, that can be used if needed, but ignored if not. 
While using typing is indeed not mandatory, once used, it *is* mandatory 
check that can not be ignored and produces fatal error in case of 
mismatch. The only difference is that in Java this error is detected in 
compile time, while in PHP it is a runtime failure, usually taking down 
the application in the middle of processing, without possibility of 
proper recovery. I'm not sure we're having the better deal here.
Performance is irrelevant here, more or less, since typing checks won't 
take much time unless you have insanely deep class hierarchies (you have 
to compare object's class and probably his parents' to the prescribed 
type). Stability and maintainability of such code is an issue.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #57009 — Rasmus Lerdorf — 2011-12-22T18:41:28+00:00

URL: https://externals.io/message/56974#57009 · In reply to: #57000

> i would love to see this expanded aswell (the way type hinting on function
> variables was supposed to be), so that it could be
> 
> string, int
> 
> e.g.
> function int test(bool $tester) {
>  if ($tester) { return 5; }
>  return 99;
> }

Return type hinting needs to be aligned with parameter type hinting, and
as has been pointed out many times on this list, type hinting for
interchangable scalar types is a really bad idea. It will push all type
checking up to the caller of the underlying functions/methods. PHP is
primarily a Web scripting language and the Web isn't typed. Having stuff
like this break:

if(age_check($_POST['age'])) { do_stuff(); }

because the author of the age_check() function added an int type hint
just doesn't make any sense. It would cause everyone to have to start
casting things everywhere, just in case. eg.

if(age_check((int)$_POST['age'])) { do_stuff(); }

This is not a step forward. If the author of age_check() really doesn't
want to accept type-juggled arguments, then it is easy enough to do a
strict type check in the function itself. This puts the effort in the
correct place and doesn't encourage this type of coding.

-Rasmus

---

## #57010 — Paul Dragoonis — 2011-12-22T18:50:53+00:00

URL: https://externals.io/message/56974#57010 · In reply to: #57009

> 
> > […]
> function
> > […]
>
> Return type hinting needs to be aligned with parameter type hinting, and
> as has been pointed out many times on this list, type hinting for
> interchangable scalar types is a really bad idea. It will push all type
> checking up to the caller of the underlying functions/methods. PHP is
> primarily a Web scripting language and the Web isn't typed. Having stuff
> like this break:
>
> if(age_check($_POST['age'])) { do_stuff(); }
>
> because the author of the age_check() function added an int type hint
> just doesn't make any sense. It would cause everyone to have to start
> casting things everywhere, just in case. eg.
>
> if(age_check((int)$_POST['age'])) { do_stuff(); }
>
> This is not a step forward. If the author of age_check() really doesn't
> want to accept type-juggled arguments, then it is easy enough to do a
> strict type check in the function itself. This puts the effort in the
> correct place and doesn't encourage this type of coding.
>

I agree with Rasmus.

My opinion:
This isn't java, if you want a strongly typed language, there's plenty out
there, but we're not looking to make PHP more like Java and the rest, if it
was like Java then it wouldn't have been successful for the web as it
is/was. PHP is popular because of the way it was from the start, if php
_needed_ scalar typehints then it wouldn't have been as popular.

There is need for the existing Typehinting for class types so you don't
need to have `is_a()` function calls. This makes sense.

Scalars are VERY powerful in PHP, because of its loose typed nature. Having
a 'numeric' typehint makes sense, because it can be an int, float, or
string. Adding typehints like 'int' and 'float' will only piss people off,
and make PHP more difficult and less fluent to code in.

Lets not go there please..

Thanks,
Paul Dragoonis.



>
> -Rasmus

---

Next page: https://externals.io/message/56974.md?page=2
