|  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
Avg. Score:4.9 ± 0.3
Reproduced:16 of 18 (88.9%)
Same Version:11 (68.8%)
Same OS:8 (50.0%)
From: corey at eyewantmedia dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-08-03 18:10 UTC] corey at eyewantmedia dot com
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:
	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();

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


Add a Patch

Pull Requests

Add a Pull Request


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 



$test = (int)4;

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]
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:

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:

That code does not run; it produces the same error as my sample class code.
 [2004-08-03 18:46 UTC]
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 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 - [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, 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]
File is updated now
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat May 28 17:05:46 2022 UTC