#29508 [NEW]: Primitive types not allowed in type hinting of function arguments

From: Date: Tue, 03 Aug 2004 16:10:30 +0000
Subject: #29508 [NEW]: Primitive types not allowed in type hinting of function arguments
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-64025@lists.php.net to get a copy of this message
From:             corey at eyewantmedia dot com
Operating system: winXP Pro
PHP version:      5.0.0
PHP Bug Type:     Zend Engine 2 problem
Bug description:  Primitive types not allowed in type hinting of function arguments

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 bug report at http://bugs.php.net/?id=29508&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=29508&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=29508&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=29508&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=29508&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=29508&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=29508&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=29508&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=29508&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=29508&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=29508&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=29508&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=29508&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29508&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=29508&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=29508&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=29508&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=29508&r=float


Thread (8 messages)

« previous php.bugs (#64025) next »