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

 ID:                 53095
 Updated by:         ras...@php.net
 Reported by:        justinmitchell at iinet dot net dot au
 Summary:            Optional type hinting [strong typing] on variables
-Status:             Open
+Status:             Bogus
 Type:               Feature/Change Request
 Package:            Java related
 Operating System:   Nix/Win
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2010-10-20 10:46:23] 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-19 12:53:19] 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-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

Reply via email to