Req #53095 [Com]: Optional type hinting on variables

From: Date: Tue, 19 Oct 2010 10:53:19 +0000
Subject: Req #53095 [Com]: Optional type hinting on variables
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155326@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=53095&edit=1

 ID:                 53095
 Comment by:         uramihsayibok at gmail dot com
 Reported by:        justinmitchell at iinet dot net dot au
 Summary:            Optional type hinting on variables
 Status:             Open
 Type:               Feature/Change Request
 Package:            Java related
 Operating System:   Nix/Win
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

What you're suggesting is less "type hinting" and more "strict typing",
which goes 
against what PHP has been doing for years. Consider using Java instead.

See also the PHP wiki (specifically
http://wiki.php.net/rfc/typechecking) and 
previous feature 
requests/bug reports.


Previous Comments:
------------------------------------------------------------------------
[2010-10-18 11:26:25] justinmitchell at iinet dot net dot au

Description:
------------
I know there's been a lot of discussion about type hinting for years
now. It's prevalent in most OOP languages and as PHP makes the move
towards being an OOP scripting language, it would make sense to
introduce certain language features. Unless there is a technical
constraint (quite possible), would it be possible to introduce type
hinting on variables? 
 
I make the argument because it would be encourage data validation based
on the variable type rather than having to create functions to check if
the input matches the variable type. It would vastly improve current and
future frameworks, reduce code and improve error handling (assuming the
programmer implements).

Cast exceptions would need to be implemented as well, eg. String could
not be cast to int, int cannot be set a string value etc.

It can also in part replace the is_int, is_numeric, is_bool etc.
functions; functions which wouldn't be needed if there was variable type
hinting.

Test script:
---------------
<?php
$a = 0;
int $b = 0;

changeVar(&$var) {
    try {
        $var = 'abc123';
    } catch (Exception $ex) {
        echo 'could not change variable value or something';
    }
}

Expected result:
----------------
There will be a compile time error because variable type hinting isn't
supported. If it were to be implemented you would expect something along
the lines of:

changeVar($a);
changeVar($b);
// prints exception message

echo $a; // abc123
echo $b; // 0

Actual result:
--------------
Throws a compile time error, isn't supported by php


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53095&edit=1


Thread (4 messages)

« previous php.bugs (#155326) next »