Req #66886 [NEW]: extend type hint to base types, class members and variables

From: Date: Wed, 12 Mar 2014 07:57:20 +0000
Subject: Req #66886 [NEW]: extend type hint to base types, class members and variables
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184735@lists.php.net to get a copy of this message
From:             matteo_tassinari_TM at libero dot it
Operating system: Irrelevant
PHP version:      Irrelevant
Package:          Unknown/Other Function
Bug Type:         Feature/Change Request
Bug description:extend type hint to base types, class members and variables

Description:
------------
I would like to suggest a new feature for a future version of PHP, for
which I did not find any entry in the RFC wiki.

Basically, it could be useful to extend the type hinting for functions
and methods also to standard PHP types (int, bool, float, etc.) and also
allow for multiple acceptable types.

For example:

function foo(int|string $parameter = NULL) { ... }

Here $parameter MUST be one of NULL, int or string, or it would trigger
a fatal error.

I think that the interpreter should not do any type juggling in this
case, or only for the most simple and potentially less dangerous, for
example bool to int, int to float or float to int (with warning in this
case), but not for example string to int.

Also, the default value for a parameter should be consistent so that for
example:

function bar(int $param = "") { ... }

would cause a fatal error too.

At the same way it could be useful to typehint variables and class
properties, so that a "declaration" like:

int $i = 0;

would trigger a fatal error if, at any later point, $i is assigned
something different than an integer, the same would apply to:

class C {
  public int $i = 0;
}

In this cases, it could be useful to have multiple hints too, like

class C {
  public int|string $xxx = 0;
}

And it would be up to the interpreter to enforce the type upon
assignment.

There is already an RFC for allowing a type hint like
"Class_Name_Here[]" for an array of a specific type, and that RFC could
be extended also in this case, so that a type hint of "int[]" would be
acceptable.

Lastly, such new feature should be completely backward compatible, as it
would be optional to use it, and there cannot be any current code like
that.

Test script:
---------------
See description


-- 
Edit bug report at https://bugs.php.net/bug.php?id=66886&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66886&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66886&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66886&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66886&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66886&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66886&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66886&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66886&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66886&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66886&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66886&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66886&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66886&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66886&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66886&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66886&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66886&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66886&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66886&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66886&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#184735) next »