php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74289 Strange behavior when casting the types of functions/methods
Submitted: 2017-03-22 11:14 UTC Modified: 2017-03-25 11:43 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 Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or 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

Add a Patch

Pull Requests

Add a Pull Request

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>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Mar 26 00:01:27 2019 UTC