php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29508 Primitive types not allowed in type hinting of function arguments
Submitted: 2004-08-03 18:10 UTC Modified: 2004-08-03 19:11 UTC
Votes:14
Avg. Score:4.9 ± 0.3
Reproduced:11 of 13 (84.6%)
Same Version:8 (72.7%)
Same OS:6 (54.5%)
From: corey at eyewantmedia dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-08-03 18:10 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-03 18:18 UTC] 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:24 UTC] 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:39 UTC] helly@php.net
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:40 UTC] corey at eyewantmedia dot com
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.
 [2004-08-03 18:46 UTC] derick@php.net
That is not documentation at all, but a very outdated article.
 [2004-08-03 19:04 UTC] corey at eyewantmedia dot com
Maybe that should be made a bit clearer.

I'm not being cynical, I am trying to help. I'd like to explain how I came to think it was documentation.

As a new user to php, I naturally visitied php.net in sarch of some answers.

The link in the first article on the page says to check out zend II engines new features, and I follow the link. The first link on that page says 

'Changes in PHP 5/Zend Engine II
By php.net - [March 22, 2004]
A well-organized listing of the changes and their use.'

I follow that and find an outdated article. That article is referenced by the most recent article on php.net, so there is no reason for me to think it is outdated.

Perhaps a notice should be posted on the article itself, or the link should be moved off the front page of php, or some notation should be made near the link to let people know that it is outdated.
 [2004-08-03 19:11 UTC] helly@php.net
File is updated now
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 25 07:02:14 2014 UTC