Req #69486 [Opn->Wfx]: Add support foir _toBoolean magic method
| From: | cmb@php.net | Date: | Thu, 19 Aug 2021 15:05:22 +0000 |
| Subject: | Req #69486 [Opn->Wfx]: Add support foir _toBoolean magic method | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235963@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69486&edit=1
ID: 69486
Updated by: cmb@php.net
Reported by: adrian at adecsys dot com
Summary: Add support foir _toBoolean magic method
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Class/Object related
Operating System: *
PHP Version: 5.4.40
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Seems there is not much interest in this feature, and given that
the details have not been figured out, I'm closing this as
WONTFIX.
Anybody who is looking for a ::__toBoolean() magic method or
something like this, is welcome to pursue the RFC process[1].
[1] <https://wiki.php.net/rfc/howto>
Previous Comments:
------------------------------------------------------------------------
[2015-05-07 17:01:13] cmb@php.net
Related To: Bug #69594
------------------------------------------------------------------------
[2015-04-27 23:02:35] cmb@php.net
Proposal A would constitute a BC break, though. Consider existing
code relying on boolean type juggling (or a cast) to test for
object existence, where the object has a __toString method
returning a "falsy" value (like in the given test script).
With regard to proposal B: how is int (or float) juggling, for
instance, supposed to work?
(int) $obj->__toString()
(int) $obj->__toBoolean()
(int) $obj
------------------------------------------------------------------------
[2015-04-20 08:02:26] adrian at adecsys dot com
Description:
------------
---
From manual page: http://www.php.net/language.oop5.magic
---
The inbuilt conversion of objects to Boolean, especially for expression evaluation, is poor and
could be greatly improved.
I propose one of two mechanisms:
(a) Given the existing __toString() magic method, use this as an intermediate step in the conversion
to a bool value, following the documented rules for string.
or
(b) add support for an explicit __toBoolean magic method, which allows the implentation to take full
control over the resulting type.
Thank you
Test script:
---------------
class NullObject {
public function __toString() { return ""; }
public function __toBoolean() { return false; }
public function __call($name , $arguments) { return $this; }
}
$test = new NullObject();
echo $test ? "TRUE" : "FALSE" ;
// TRUE, but logically expect FALSE
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69486&edit=1