Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111405 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70933 invoked from network); 9 Aug 2020 22:00:29 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Aug 2020 22:00:29 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B1A141804D9 for ; Sun, 9 Aug 2020 13:59:26 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10olkn2090.outbound.protection.outlook.com [40.92.41.90]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 9 Aug 2020 13:59:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mwOeyD5h5KaaoV0Nmw19zhxXumm++JJK/83ONfaoytf7mxcBiC5pXhgi92nWb3fpRPUMzLoURIFcYtMAo/mKKIbsijvyjGEOm77YBGUv+OH21DpkanMH1HCQ3YMte98R6L2LO32DgTxXqvpIWvleJfgFOge7vpXIq+qm3WMU3OOr7PZ3gHcnMzjHN09hvoTItIPH1+ElsB4AWgv1KnrzzSNefwFXPitCtu9lfegxhGyZdCdSmmu02OzwUDwDJ7ZugKfAkV3DlLfml7podF6aB3ul6QY7EmgnIh6wpvjZcIw6ea59yn+Pp6VQgjbj9y0FU+/4iZpzIOospiD1BW+3wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9V2MIg2wkVZezEK33YdqoUt498rxZ8cWXgiBMkA5jcY=; b=SqRcJabOSKaoCNibSUFyz4JeqrwQU4o1ANI7gLxfgkQ+bbh9CNKqd+B4xxhxwy7k2sUY4h7jWP1646IRYva7xmIS5FENidgj+rp5hO1ojwDVc4CQR99dBhK8dO7eMtDmQESBOl97CpLD7zhlfFGYzFWMCHTCwEK6CuJ2JKG1dmRJSObnUJHLSMOHfFT+cVZ8o8zu+fjAUFeBdwsSvq+PoymbBdbMxj0Qe/IfViEMZt+xGmiAfEtZpkORi99ShbWlf5Mj9oiU/heXzM7dfmmnl5PfMo/mlEsY12jqFtVSuX/bC21Erxr2gyZr6ypNoK4S5tPLm+jDkqeo6cFh6RbHcw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9V2MIg2wkVZezEK33YdqoUt498rxZ8cWXgiBMkA5jcY=; b=OzKuf4vVt7ZPAyZrRWjGw5YqMyZmBy7fxpLmwyEY0WbU08xmx3id5f1wtRiVwcVYldArQVLcMdb1PFoAh9+RH3oWe226Hg+n/9FtH9tv234jusAokaT/+isxRdVjJprC1Hqm0KVegOOUCjdu1/ZM2KsaxyIhKbR6xjm5OY5MdMIVPZqHZWf3p5TJd8MtDIxrj0q2eyygTAx7w+Dx5Hj1pR6rouW6Nh9D1J41iEN2w1cZCUwaRycHb4Wj/OWjENwYk35swe1r6nN1SPZXbcM8VXvzAXgAYXnF1HI4tYujMB2r8/IxHNMTeVjcDnRsEIWHNiztalRh67b2H2bGXDGOuA== Received: from MW2NAM10FT027.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e87::52) by MW2NAM10HT096.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e87::353) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.16; Sun, 9 Aug 2020 20:59:25 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e87::4d) by MW2NAM10FT027.mail.protection.outlook.com (2a01:111:e400:7e87::443) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.16 via Frontend Transport; Sun, 9 Aug 2020 20:59:25 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::cc10:a3e2:1dcf:adc1]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::cc10:a3e2:1dcf:adc1%6]) with mapi id 15.20.3261.022; Sun, 9 Aug 2020 20:59:25 +0000 To: Josh Bruce , Larry Garfield CC: php internals Thread-Topic: [PHP-DEV] [RFC][Discussion] Objects can be declared falsifiable Thread-Index: AQHWbobn4QIbscesJkuG+LU+Gtbl6qkwOruR Date: Sun, 9 Aug 2020 20:59:25 +0000 Message-ID: References: <21C5073D-3F39-49DA-8686-E027AE780793@joshbruce.dev> <1bc7759a-63b8-4b96-bb69-97b50f851307@www.fastmail.com>, In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:E07935631FA85BB42235FA0B2339C64D24069AF766C9721A342204C932DD7286;UpperCasedChecksum:C6EB80AF4CB582A0DEDBE7454F3CB200AC7DA0F5494EA601DD1D219A659F1B83;SizeAsReceived:7273;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [iuD0/pCkEUNFl6DDzN2i0ftj//gfGdpEKZQR3Ohywa/Uqgs6Fh3u3X1oLemmDRoK] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: d971de9c-5084-47ce-45b7-08d83ca71924 x-ms-traffictypediagnostic: MW2NAM10HT096: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: vUySiDN8Wq/hSNCfhD8YcHDvb5b1cJb/YuL9R4BYGOKcdNk8Bzq3EA9CybWMVvkct+QVR1Bz2Tt2g64B31D2MR/PpvjbrqS4mWRiq9DdSHCVaWGELdYw8KrS8EdLDFxHs/o0rkHlTk4JNmollLMaf0RY2aw6jhTkW59+IbXWIrWPK6N+jUu4fsOXTX0hPuNQoVDKBEdpbajwALdjV9i6TCBP6g408psn98hfihdQJEur6J2Ki55YdXXRzl61a2bk x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:0;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR07MB6618.namprd07.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:;DIR:OUT;SFP:1901; x-ms-exchange-antispam-messagedata: +16t+UT3MWzMjDImoHRWrV2ZnMAMSO8LakiRWBhbAhqAbdZfploPan4SnesMUaUX7k0pDm/HcitdLrNJAg0DJEqgAByokC9KHxUFSkaPSJJkz3ueZIApqlfrZOyWOw5CWeU4JcIPXv+L1RAR5hWmSIhACw+HlYurmLKTljrqg8dw6+zx0xNW9btLsvPbO1vve1lwCHPiGSetwD4u0uyJoA== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: MW2NAM10FT027.eop-nam10.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: d971de9c-5084-47ce-45b7-08d83ca71924 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Aug 2020 20:59:25.1328 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2NAM10HT096 Subject: Re: [PHP-DEV] [RFC][Discussion] Objects can be declared falsifiable From: tysonandre775@hotmail.com (tyson andre) Hi Josh,=0A= =0A= I'd recommend looking at https://github.com/php/php-src/pull/5156 (linked t= o in your RFC's reference) when implementing this PR.=0A= Additionally, I didn't see if this was brought up - PHP already has a class= which is falsifiable - SimpleXMLElement.=0A= =0A= You can see the source of ext/simplexml/simplexml.c for `_IS_BOOL`=0A= =0A= ```=0A= static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int typ= e)=0A= {=0A= php_sxe_object *sxe;=0A= xmlChar *contents =3D NULL;=0A= xmlNodePtr node;=0A= int rv;=0A= =0A= sxe =3D php_sxe_fetch_object(readobj);=0A= =0A= if (type =3D=3D _IS_BOOL) {=0A= node =3D php_sxe_get_first_node(sxe, NULL);=0A= if (node) {=0A= ZVAL_TRUE(writeobj);=0A= } else {=0A= ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));=0A= }=0A= return SUCCESS;=0A= }=0A= ```=0A= =0A= ```=0A= static php_sxe_object* php_sxe_object_new(zend_class_entry *ce, zend_functi= on *fptr_count)=0A= intern->zo.handlers =3D &sxe_object_handlers;=0A= // ... elsewhere in ext/simplexml/simplexml.c=0A= sxe_object_handlers.cast_object =3D sxe_object_cast;=0A= ```=0A= =0A= The default handlers would be overridden, so this would probably need to fa= ll back to the original handlers for types other than bool.=0A= Also, this might need to copy all of the handlers from the parent class ent= ry's zend_object_handlers,=0A= to meet the expected behavior of other cast types and other magic behaviors= from SimpleXMLElement and other classes continuing to work.=0A= =0A= From the perspective of static analysis, a few things to note:=0A= - Static analyzers such as psalm/phan currently deliberately don't bother h= andling the possibility that `object` can be false even after it was checke= d for falsiness.=0A= There's hundreds of other things that could be implemented, and adding th= e special casing and performance overhead checking for FFI objects and Simp= leXMLElement, subclasses of those is currently low priority compared to tho= se things.=0A= =0A= Code using SimpleXMLElement/FFI is generally limited to a few files in pr= actice.=0A= =0A= Definitely possible for analyzers to support this for known base classes,= though, and the priority would increase if the RFC passed.=0A= =0A= I am a maintainer of Phan.=0A= - For BC reasons, internal data structures such as ArrayObject probably wou= ldn't get changed in any php release=0A= (e.g. could break code using falsiness check instead of null check).=0A= So this might lead to inconsistencies with newer extensions if half of t= he data structures treat emptiness as false and half don't.=0A= - This feature may end up getting adopted in cases where it's convenient bu= t turns out prone to accidental bugs and later is deprecated and removed.= =0A= For example, https://stackoverflow.com/questions/25031236/if-elem-vs-elem= -is-not-none was seen in python=0A= =0A= (e.g. `if (!$this->resultSetObject) { $this->resultSetObject =3D slow_db_= operation(); }`=0A= would not behave the way people would previously expect for most objects = (executed repeatedly instead of once))=0A= =0A= ```=0A= And I don=92t know of a way to add an interface to new stdClass() - but t= hought this might be a valid use case:=0A= =0A= `stdClass` isn't a final class. I assume they meant this=0A= =0A= ```=0A= class EmptyStdClass extends stdClass implements Falsifiable {=0A= public function __toBool() { return false; }=0A= }=0A= function example(stdClass $s) {=0A= if (!$s) { throw new Exception("Previously impossible"); }=0A= }=0A= ```=0A= =0A= Thanks,=0A= - Tyson=