Web lists-archives.com

Re: [PHP] [PHP7][Bug?] What is the difference between 'boolean' and 'bool'?




On Wed, 16 Dec 2015 12:51:39 -0800 David Harkness <david.h@xxxxxxxxxxxxxxxxx> wrote:
> 
> Code (example4):
> 	function bar(boolean $foo): boolean{return $foo;}
> 	var_dump(bar((bool)true));
> Output:
> 	PHP Fatal error:  Uncaught TypeError: Argument 1 passed to bar() must
> 	be an
> 	instance of boolean, boolean given, called in ./example4.php:2

The actual type is “bool", but “boolean" has always been an alias; there are a few of these in PHP (such as “int" v. “integer"). In PHP 5, the types and their aliases are interchangeable everywhere, but in the case of the new scalar type hinting and return-type features in PHP 7 (which is what you’re showing), the aliases are simply not supported. The error you’re seeing is trying to tell you that bar() expects an instance variable of a class named “boolean”, but it actually received a scalar variable of type “bool" (which it referred to as “boolean” because error messages use the long form names to be more friendly). It makes sense when you know what’s going on, but IMHO, it’s a rather unfortunate inconsistency that will lead to a lot of confusion the first time people encounter it, just as it did in your case. I hoped things would change before release, but that didn’t happen. You can read that discussion here:

http://www.serverphorums.com/read.php?7,979538,page=8

Basically, the internals team's stance is that aliases are already an inconsistency, so we should be working toward removing them, not adding support for them in new features.

PHP 7 is a wonderful release into which a lot of folks put a lot of effort, but this was one particular decision with which I really didn't agree. But, as I said, once you’re aware of it, it’s not hard to avoid even if it means breaking some old habits or changing code standards around use of aliases.

--
Bob Williams
Business Unit Information Officer and
Senior Vice President of Software Development
Newtek Business Services Corp.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail