php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76964 Return type-hint actually cast types
Submitted: 2018-10-02 17:55 UTC Modified: 2018-10-02 20:50 UTC
From: itay dot moav at email dot sitel dot org Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.2.10 OS: centos7
Private report: No CVE-ID: None
 [2018-10-02 17:55 UTC] itay dot moav at email dot sitel dot org
Description:
------------
The following code will return 1
I would expect it to fail

class A{
 static public function r():bool{
  return 'ww';
 }
}

Test script:
---------------
class A{
 static public function r():bool{
  return 'ww';
 }
}

var_dump(A::r());

Expected result:
----------------
I expect this call to fail with proper error message

Actual result:
--------------
casts the return value to 1 (int)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-02 18:08 UTC] pmmaga@php.net
-Status: Open +Status: Not a bug
 [2018-10-02 18:08 UTC] pmmaga@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

It would only fail if you use strict_types. You can find more info on http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict
 [2018-10-02 18:08 UTC] requinix@php.net
-Status: Not a bug +Status: Open
 [2018-10-02 18:08 UTC] requinix@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://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

"Strict typing also has an effect on return type declarations. In the default weak mode, returned values will be coerced to the correct type if they are not already of that type. In strong mode, the returned value must be of the correct type, otherwise a TypeError will be thrown."

Either way you don't get the integer 1.
 [2018-10-02 18:09 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-10-02 18:09 UTC] requinix@php.net
bleh
 [2018-10-02 19:52 UTC] spam2 at rhsoft dot net
it's one thing taht you dont't understand strict typing but what you repoted is simly not true and WHERE is the execution part of your snippet?

[harry@srv-rhsoft:/downloads]$ cat test.php
<?php
class A{
 static public function r():bool{
  return 'ww';
 }
}

var_dump(A::r());
?>

[harry@srv-rhsoft:/downloads]$ php test.php
bool(true)
 [2018-10-02 19:54 UTC] requinix@php.net
OP likely originally used print_r, which shows true as "1" and false as "", then changed the code to var_dump.
 [2018-10-02 20:50 UTC] nikic@php.net
Last comment by spam2 at rhsoft dot net deleted, due to use of unnecessarily antagonistic language without contributing any valuable insight for this bug report.

Please refrain from such comments in the future.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 06:01:30 2024 UTC