Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108368 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 76520 invoked from network); 4 Feb 2020 14:43:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 14:43:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 948EE180533 for ; Tue, 4 Feb 2020 04:55:27 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 4 Feb 2020 04:55:27 -0800 (PST) Received: by mail-wm1-f53.google.com with SMTP id s144so2157289wme.1 for ; Tue, 04 Feb 2020 04:55:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inventis.be; s=google; h=date:from:subject:to:message-id:in-reply-to:references:mime-version; bh=ASlBybygywuyK1Ib1oJR2xgO3YG7viKssVGd+GzWxqU=; b=b+37t2x4JaWdiQbzbwrrpprPIJv7u+A35O8/7aBgZRPE1kRe20csjmwDS7p0nat9vw Tq2EBGmoXbLh10Z0nuDLvCvtG+KmP2E728UAjYhHdebnB1rBaI2uGMMv2wXBeN88/+t6 vnaKTADlxlVnZYBfF8K1O1CGK8401dDfeUUoaXSPJVhH9Z4zw4u8S/mjY2+5H2SvM5vK evm2mGVO7cDt5zK2ua8WeuNEG9sSzKw2J8RGvZVhHrb07aBiLVaVOGnYUBfbsDdPKoIa 8SGmLZDBcHnTMQJLoRDkB3hxDvMLs/thd+jirGXOI3t9PwtlTqZHcRF1c4sCZ1SOr3zt hZfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:to:message-id:in-reply-to :references:mime-version; bh=ASlBybygywuyK1Ib1oJR2xgO3YG7viKssVGd+GzWxqU=; b=Fb8B0/xUH/XM9RiFoe5dmm/np+B1Af6+wy6Ep0fJuc1J5dYYZhTdvzc6b6x4Wjo/yC 4qmyWlHvHafIfJpQYJLhhbUQyABn000ATyqXeYQUqr9Kyvhtk8h/vrttlrSgNEyAzhgZ YugMZCMZBblO7xfOc/g/wJEX0Y66AwBWYkv+4i3wAb7pE2B80xKTdnl0Ms6PpN4fS08C xZ9qPx0doYuDbF+2fBz+rR4oCVhe1NhMKiYAiw4jOVFPxELovVaG/qW1lOKFDdycHFvF UqsecQjXDK6jSet7bkvsO8x43hhZ7CmEFRwF4IS5ocV9uW9EctbRbWNkdwvZvByHD7Wj EMjw== X-Gm-Message-State: APjAAAVIdj13dFNa3R/qPzG9s++Gi98Jf/uwqVlTVOy7PgP2T2YhjMdw sdN2ORmog0wb8kEl+zf2wYv/Bxzv107AkcTo X-Google-Smtp-Source: APXvYqxgmY9okjGP6KSxYvUBXuwl4b6+1JQed+4J0AAH5G36sQPv6INoruAjiHHsPbVAv9BAyYJ2MA== X-Received: by 2002:a1c:9897:: with SMTP id a145mr5531092wme.132.1580820925800; Tue, 04 Feb 2020 04:55:25 -0800 (PST) Received: from hephaestus (d5152db11.static.telenet.be. [81.82.219.17]) by smtp.gmail.com with ESMTPSA id a9sm3580983wmm.15.2020.02.04.04.55.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2020 04:55:25 -0800 (PST) Date: Tue, 04 Feb 2020 13:55:23 +0100 To: internals@lists.php.net Message-ID: <1580820923.58383.2@inventis.be> In-Reply-To: References: X-Mailer: geary/3.34.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Subject: Re: [PHP-DEV] What about a magic __toArray() method? From: tom@inventis.be (Tom Gerrits) > Correct: an explicit `$foo->toArray()` call suffices. > > For other readers, more about `$foo->toArray()` vs `(array) $foo` is > described at > > https://github.com/ShittySoft/symfony-live-berlin-2018-doctrine-tutorial/pull/3#issuecomment-460441229 This is a valid point. Perhaps another problem lies at its base: why can one cast null to a string in PHP? Would it be more appropriate to throw a CastError or similar? Or does casting null to another type make sense in some scenarios? One could go as far as to only allow types that overload the appropriate cast operator to cast to the type without error. This implies the question of how to handle built-in types, such as casting floats to string. > - It is less explicit than choosing a purpose-specific method name I think it's also important to mention whether this discussion is about explicit casts only, or not - and also about implicit casts, or coercion. "(string) $var" doesn't seem a lot less explicit to me than "$var->toString()", but if it is coerced automatically when being passed to e.g. a function taking a string, that is of course a different story. Coercions and implicit casting using the overloaded operators could perhaps also be limited to non-strict mode, if desired, which is also the case for scalar types currently, IIRC.