|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69718 Return values for magics can be nonsensical
Submitted: 2015-05-28 07:05 UTC Modified: 2020-08-02 04:43 UTC
Avg. Score:3.2 ± 1.5
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Assigned: carusogabriel (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: master-Git-2015-05-28 (Git) OS: *
Private report: No CVE-ID: None
 [2015-05-28 07:05 UTC]
Since we now allow defining return value types for functions, one can also create definition for magic functions that make no sense, such as __toString() : float or __isset() : Closure. I think we should restrict such definitions - either to types that makes sense or in case of methods that aren't supposed to return anything useful, completely. We already do this for ctors, dtors and __clone but not consistently for other magics.

Specifically, the following should be true:

__clone - no return type
__isset - only boolean allowed as return type
__sleep - only array allowed as return type
__set - no return type
__toString - only string allowed as return type
__wakeup - no return type

Also, may make sense to restrict argument types (of course, not typing is always ok):

__call/__callStatic - only string for first and array for second arguments
__get/__set/__isset/__unset - only string for the first argument

__set_state - only array is allowed as argument type


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-28 07:06 UTC]
-Assigned To: +Assigned To: stas
 [2019-05-31 03:38 UTC]
-Assigned To: stas +Assigned To: carusogabriel
 [2019-05-31 03:38 UTC]
The following pull request has been associated:

Patch Name: Ensure correct signatures for magic methods
On GitHub:
 [2020-04-26 03:14 UTC]
> __set_state - only array is allowed as argument type

Btw, as reported via Bug #79521, this is not actually the case, we can return `mixed`.
 [2020-08-02 04:43 UTC]
-Status: Assigned +Status: Closed
 [2020-08-02 04:43 UTC]
PR was merged and this bug is now solved in PHP 8.0.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 19 15:01:31 2024 UTC