php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40782 only arrays and objects can be passed as typed args.
Submitted: 2007-03-12 06:04 UTC Modified: 2007-03-13 04:15 UTC
From: tims at arizona dot edu Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.1 OS: Debian Linux
Private report: No CVE-ID: None
 [2007-03-12 06:04 UTC] tims at arizona dot edu
Description:
------------
I have found a minimal reproducible test case that consistently fails. I believe this should work as a typed parameter but php5 dies with the result below. 


Reproduce code:
---------------
$b = true;
function f( boolean $a ) { return $a; }
f( $b );

Expected result:
----------------
The above code does not do much except return the boolean that was passed in. My larger block of code uses typed arguments to ensure the type of data being passed in. The bool type does not work and I can not find a suitable workaround.



Actual result:
--------------
Catchable fatal error: Argument 1 passed to f() must be an instance of boolean, boolean given, called in /home/bink/Projects/opticsnow/web_software/dev_space/a.php on line 5 and defined in /home/bink/Projects/opticsnow/web_software/dev_space/a.php on line 4


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-12 06:15 UTC] judas dot iscariote at gmail dot com
I indeed think that booleans should be accepted, but this is not a bug, but a feature request :) actually type hinting works for Objects and arrays **only**.

There is a patch to allow this IIRC but has not been merged into,and probably will only be included with PHP6.
 [2007-03-12 08:49 UTC] bjori@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://devzone.zend.com/node/view/id/1518#Heading8
 [2007-03-13 04:15 UTC] tims at arizona dot edu
For anyone that google's the thread, and wants a work-around you might try something like:

function f( $bool_arg ) {
  if( ! is_bool( $bool_arg ) )
    throw new Exception("Argument is not boolean.");

  .. rest of function declaration ..

}

at least until php6.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Dec 26 23:00:01 2025 UTC