php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76382 There's no way to override conversion to Boolean, yet SimpleXMLElement does it
Submitted: 2018-05-27 16:50 UTC Modified: 2018-05-27 21:14 UTC
From: teo8976 at gmail dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 7.2.6 OS:
Private report: No CVE-ID: None
 [2018-05-27 16:50 UTC] teo8976 at gmail dot com
Description:
------------
---
From manual page: http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
---

""""""""
When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
the special type NULL (including unset variables)
SimpleXML objects created from empty tags <<<<<<<<<<<<< THIS!!!!

Every other value is considered TRUE (including any resource and NAN).
""""""""

The SimpleXML class is "magical" in that it overrides the general rule that any non-null object evaluates to true, and it has its own way of converting to boolean.

Yet there's no (documented and non-hacky) way a user-defined class can do the same and define its own rules to convert to boolean. There's no __toBoolean() (like there is a __toString()) magic method, and there's no operator overloading for overloading the == operator.

This is DISGUSTING. That a built-in class can do some magic that no user-defined class can do, is a symptom of an incredibly poor language design. The very moment you decided that non-null SimpleXML objects should evaluate to false in certain cases, you should have realized that any user-defined class could need to do the same thing, and you should have provided a generic mechanism of doing that, rather than hard-coding some magic into a particular built-in class.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-27 17:00 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2018-05-27 17:00 UTC] requinix@php.net
My deepest apologies that you find PHP to be so repulsive.

There are already multiple RFCs for such a feature but if you wish to start another one then you should read the how-to: https://wiki.php.net/rfc/howto
 [2018-05-27 20:33 UTC] teo8976 at gmail dot com
I see you marked this as duplicate, but I don't see any link to the bug of which this is a duplicate.
 [2018-05-27 21:14 UTC] requinix@php.net
How about bugs 72579, 70604, 65893, 55852, 52583, 48076, and 46128?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun May 26 04:01:27 2019 UTC