Bug #76382 [Opn->Dup]: There's no way to override conversion to Boolean, yet SimpleXMLElement does it

From: Date: Sun, 27 May 2018 17:00:32 +0000
Subject: Bug #76382 [Opn->Dup]: There's no way to override conversion to Boolean, yet SimpleXMLElement does it
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215390@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76382&edit=1 ID: 76382 Updated by: requinix@php.net Reported by: teo8976 at gmail dot com Summary: There's no way to override conversion to Boolean, yet SimpleXMLElement does it -Status: Open +Status: Duplicate Type: Bug Package: *General Issues PHP Version: 7.2.6 Block user comment: N Private report: N New Comment: 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 Previous Comments: ------------------------------------------------------------------------ [2018-05-27 16:50:27] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76382&edit=1

« previous php.bugs (#215390) next »