ID: 29508
User updated by: corey at eyewantmedia dot com
Reported By: corey at eyewantmedia dot com
Status: Open
Bug Type: Zend Engine 2 problem
Operating System: winXP Pro
PHP Version: 5.0.0
New Comment:
New info on the problem:
if you replace the lines
$o->SetI(4);
with
$test = (int)4;
$o->SetI($test);
moves the fatal argument error from the line where $o->SetI() is called
(line 15) to the first line of the function definition (line 8), but it
is still the same error. It strikes me as odd that the $test variable
passes the type hinting int check at the place where the function is
called but not where it is executed.
Previous Comments:
------------------------------------------------------------------------
[2004-08-03 18:18:22] corey at eyewantmedia dot com
While I am thinking about it, if it is decided that primitive types
cannot be used in type hinting, can php throw an error in my function
definition rather than when I try to call it? I still think primitive
type hinting is good to have, but if it won't happen, a syntax error
would be nicer than a type checking one.
------------------------------------------------------------------------
[2004-08-03 18:10:28] corey at eyewantmedia dot com
Description:
------------
The problem is that you can't type hint a primitive type in a function
declaration and use the function with a primitive type. No primitive
types work, so every function (to be safe) has to do some sort of
if(is_int($x)) {
// do stuff
} else {
die("Must be an int!");
}
Since php scalars are so loosely typed, it seems like it would be good
practice to require a specific type of argument when the logic
contained in the function demands a specific type of variable. It saves
TONS of code.
I am running the default compile of 5.0 for windows, with mysqli, curl,
and mcrypt enabled. I have all errors printing since this is in
devlopment, but this bug is a fatal error so I think it would print out
anyway.
Reproduce code:
---------------
<?php
Class Test {
private $i;
private $d;
private $s;
private $b;
public function SetI(int $x) { $this->i = $x; }
public function SetD(double $x) { $this->d = $x; }
public function SetS(int $x) { $this->s = $x; }
public function SetB(int $x) { $this->b = $x; }
}
$o = new Test();
$o->SetI(4);
$o->SetD(4.65);
$o->SetS("yay");
$o->SetB(false);
?>
Expected result:
----------------
I expect $o->SetI(4) to accept 4 since 4 is an int (and is_int(4)
returns true), $o->SetD(4.65) to accept 4.65 since it is a double,
$o->SetS("yay") to accept "yay" since it is a string, and
$o->SetB(false) to accept false, since it is a bool.
Actual result:
--------------
Fatal error: Argument 1 must be an object of class [int, string,
double, bool] in test.php on line X
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=29508&edit=1