php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53095 Optional type hinting [strong typing] on variables
Submitted: 2010-10-18 11:26 UTC Modified: 2010-10-20 17:15 UTC
From: justinmitchell at iinet dot net dot au Assigned:
Status: Not a bug Package: Java related
PHP Version: 5.3.3 OS: Nix/Win
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: justinmitchell at iinet dot net dot au
New email:
PHP Version: OS:

 

 [2010-10-18 11:26 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-19 12:53 UTC] uramihsayibok at gmail dot com
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.
 [2010-10-20 10:46 UTC] justinmitchell at iinet dot net dot au
-Summary: Optional type hinting on variables +Summary: Optional type hinting [strong typing] on variables
 [2010-10-20 10:46 UTC] justinmitchell at iinet dot net dot au
I used the term 'type hinting' because php is a loosely typed language. Referring to it as 'strong typing' is totally against what PHP is for. It's a scripting language before anything else and as such should be treated that way. But since PHP is attempting to become partially OOP, I use this term because it's never going to be a full fledged OOP language, it might as well follow the well trodden path of many other successful languages before it.

Two examples of the same function, one of which wouldn't need to be used if the language had an option is strong typing:
http://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
http://www.php.net/manual/en/function.settype.php

I wouldn't suggest this if I didn't have a valid use for something like this - I do, unfortunately the work around means my code is far more verbose than it needs to be. Unfortunately there isn't much of an option.

I don't agree that PHP should become a strongly typed language - I agree with most arguments as for why it shouldn't, but to have the option of strong typing, it would greatly improve code across almost every project.

Example:
to create a char array
$arr = new array('a', 'b', 'c');

with strong typing:
char[] $arr = new array('a', 'b', 'c');

using anything other than the type you've specified should throw an error. As it is in some applications you need to check every element. In performance/time critical applications it's a waste of processing time.

But at the same time, the developer should have the option of using the current type-juggling variable definitions.
 [2010-10-20 17:15 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-10-20 17:15 UTC] rasmus@php.net
This discussion doesn't belong in the bug database.  Go read all the proposals 
related to this here:

http://wiki.php.net/rfc/typechecking

Then, if you think you have a better way of doing it than what has been proposed 
before, submit your own RFC to add to the 5 we already have.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC