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
 [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: Sat Dec 21 12:01:31 2024 UTC