php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69951 The function return type works for int and bool, but not for integer or boolean
Submitted: 2015-06-27 17:02 UTC Modified: 2015-06-27 17:21 UTC
From: nat at kolda dot org Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.0.0alpha2 OS: Ubuntu Linux/Apache2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nat at kolda dot org
New email:
PHP Version: OS:

 

 [2015-06-27 17:02 UTC] nat at kolda dot org
Description:
------------
The function return type works for int and bool, but not for integer or boolean.

Test script:
---------------
<?php
// This function works fine.
function foo(): int {
    return 2;
}

// This function does not work because of integer instead of int.
function bar(): integer {
    return 3;
}

// This function works fine.
function baz(): bool {
    return true;
}

// This function does not work because of boolean instead of bool.
function foobar(): boolean {
    return false;
}

echo foo(); // Works fine.
echo bar(); // Fatal error: Uncaught TypeError: Return value of bar() must be an
            //     instance of integer, integer returned in
            //     /var/www/html/index.php on line 7 in
            //     /var/www/html/index.php:7 Stack trace: #0
            //     /var/www/html/index.php(19): bar() #1 {main} thrown in
            //     /var/www/html/index.php on line 7
echo baz(); // Works fine.
echo foobar(); // Fatal error: Uncaught TypeError: Return value of foobar() must
               //     be an instance of boolean, boolean returned in
               //     /var/www/html/index.php on line 15 in 
               //     /var/www/html/index.php:15 Stack trace: #0
               //     /var/www/html/index.php(21): foobar() #1 {main} thrown in
               //     /var/www/html/index.php on line 15



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-27 17:21 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-06-27 17:21 UTC] nikic@php.net
We intentionally do not support aliases for types. You can only use bool, int, float and string. boolean, integer, double and real are not supported.
 [2021-08-12 09:20 UTC] hiiarpit at gmail dot com
<?php
// This function works fine.
function foo(): bool {
    return 'test';
}

it also work is it correct? I hope it will fix soon.
 [2021-08-12 09:36 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
> it also work is it correct? I hope it will fix soon

RTFM after that many years of strict types

https://www.phptutorial.net/php-tutorial/php-strict_types/
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 02:01:28 2024 UTC