ID: 29508 User updated by: corey at eyewantmedia dot com Reported By: corey at eyewantmedia dot com -Status: Closed +Status: Open Bug Type: Zend Engine 2 problem -Operating System: * +Operating System: winXP Pro -PHP Version: * +PHP Version: 5.0.0 New Comment:
Also, if type hinting with primitive types is not to be implemented, then the zend php5 documentation is wrong. If you look under the exceptions section, they have this snippet of code on how to extend the exception class: <?php class Exception { function __construct(string $message=NULL, int code=0) { if (func_num_args()) { $this->message = $message; } $this->code = $code; $this->file = __FILE__; // of throw clause $this->line = __LINE__; // of throw clause $this->trace = debug_backtrace(); $this->string = StringFormat($this); } //... more stuff here } ?> You can find this code here: http://www.zend.com/php5/articles/engine2-php5-changes.php#Heading12 That code does not run; it produces the same error as my sample class code. Previous Comments: ------------------------------------------------------------------------ [2004-08-03 18:39:21] [EMAIL PROTECTED] We will not provide typehints for primitive types as PHP has automatic typeconversion for them Also we are not adding any checks for primitive types since that would either slow down the compile step or would require new keywords. ------------------------------------------------------------------------ [2004-08-03 18:24:54] corey at eyewantmedia dot com 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. ------------------------------------------------------------------------ [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