php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74289 Strange behavior when casting the types of functions/methods
Submitted: 2017-03-22 11:14 UTC Modified: 2021-09-01 14:48 UTC
From: erickskrauch at yandex dot ru Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.1.3 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: erickskrauch at yandex dot ru
New email:
PHP Version: OS:

 

 [2017-03-22 11:14 UTC] erickskrauch at yandex dot ru
Description:
------------
I'm not sure if this is a bug, but when I was developing a wrapper for xml document, I ran into strange behavior when casting types in the return value of the method. I expect that the asInt() method will work just like the directConversion(), but they work differently.

Test script:
---------------
https://3v4l.org/JnFCh

Expected result:
----------------
string(3) "123"
int(123)
int(123)
int(123)

Actual result:
--------------
string(3) "123"
int(123)
int(123)

Fatal error: Uncaught TypeError: Return value of Parser::asInt() must be of the type integer, object returned in /in/JnFCh:22
Stack trace:
#0 /in/JnFCh(44): Parser->asInt()
#1 {main}
  thrown in /in/JnFCh on line 22


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-25 11:43 UTC] cmb@php.net
Simplified reproducer: <https://3v4l.org/UeRYn>. Argument type
declarations exhibit the same behavior: <https://3v4l.org/ROpEJ>.

The documentation states[1], however:

> By default, PHP will coerce values of the wrong type into the
> expected scalar type if possible.

At least this would need clarification wrt. objects implementing
::__toString().
 
[1]
<http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration>
 [2021-09-01 14:48 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC