Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108395 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56151 invoked from network); 4 Feb 2020 18:57:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Feb 2020 18:57:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6C5BC1804D1 for ; Tue, 4 Feb 2020 09:09:45 -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,FREEMAIL_FROM,HTML_MESSAGE, 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-vk1-f175.google.com (mail-vk1-f175.google.com [209.85.221.175]) (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 09:09:44 -0800 (PST) Received: by mail-vk1-f175.google.com with SMTP id w67so5383933vkf.1 for ; Tue, 04 Feb 2020 09:09:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=WIjjrTaUYkDD7dLDDTQAtXn+1+0unkxvKJdsEn6RBdk=; b=rMaqqysgyjA4sU/vGuAD7yTMNnJcWQc/lOhCAQn7hQd5BIJMW1N/r0z4q+wN59twnm c/uWOe9DxPj4bu9v5KUAihEStLTw/6EuaWrRZSN1k+v4N2lUyEFPWax7wuyKYLmdy4VR o4LK/ti1L9TPMb/6LwZ4yhvI1UjztCRUMiAXMrJHa1q50OssGhioFZbQ6sgLA74RjcxR CTM9+60A+cOaYdfQ3eIS8NGcLOAotfXKxP+Jevmjq+pfR/rloS90/YFdjlZ/VUfJVjRc v0A4dFSSNOf4LxCPyTsLO3w2mElf3Le7RsrEGpG6c2VpN7TwTlP339TRcplc9i6OWHzZ mQPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WIjjrTaUYkDD7dLDDTQAtXn+1+0unkxvKJdsEn6RBdk=; b=orAASAWRE88vyOmaZtv0qPNEHjt6mb3DsMBE8Bmae4XDrcXjHl+9tATi6xR0YP6YmD Cs6Os8z9FoeqH2AjLfTcHoHOdPluRQWd4iFhVbeoDaIhDIraZiGUgyPURqGSAfyKQQGL cQL88i18wWawumw6BKOr8hwU2mrna7OPo2hhVjiBraZQN7rjxmK6WCro7QUUlwTp7QvN MXgg6VoiZyUH0n5KT3fx7ioQv6Jpq6kCRFPgtE9GucRVuteFngGYGj000Jsy0+uzNaYm 6J5NhCzeC+j1arjICcOSy/nFFiPewYea/JYMZo6gvfJOoSgMSApA7f5qDo/oPLsTACb1 OY4Q== X-Gm-Message-State: APjAAAVLN7urx5z3X0g8IIPzV8snh+GzyPNlqdBMvOFePy8QC06lZJVt 2vAOQrBkbdchHy77V3Oapd8xXpwIiDAzGxHmR5c= X-Google-Smtp-Source: APXvYqxVWosiNLMiy0OdxnHCEo0KGQqFBP85yPkMaa8nS+HIwpETM9nWsRJxG4oyDYPyezsvdYJivRMvlyc6W7GVcWU= X-Received: by 2002:a1f:434b:: with SMTP id q72mr17821824vka.53.1580836183864; Tue, 04 Feb 2020 09:09:43 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 4 Feb 2020 12:09:32 -0500 Message-ID: To: Steven Wade Cc: Levi Morrison , Marco Pivetta , PHP Internals Content-Type: multipart/alternative; boundary="0000000000002ffc07059dc31bf3" Subject: Re: [PHP-DEV] [RFC - discussion] __toArray() From: chasepeeler@gmail.com (Chase Peeler) --0000000000002ffc07059dc31bf3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 4, 2020 at 11:04 AM Steven Wade wrote: > > > Sorry if it's been said in the discussion so far, but I do not see why > > `print_r` should convert anything to an array. It accepts multiple > > kinds of types including strings, numbers, and so on, and I think > > adding this behavior to `print_r` is a different thing than wanting a > > standard way for objects to be converted into arrays. > > You=E2=80=99re right, that=E2=80=99s my bad. I swore I tested print_r() w= ith a class and > __toString() and it cast it, but I just ran it again and it just outputs > the object. The goal would be to have __toArray() behave on arrays like > __toString() does on strings. I=E2=80=99ll update the RFC to remove the r= eference > to print_r(). Thanks! > > > > And on that note, what is the motivation for wanting a magic method > > for converting an object into an array? Why not make it an explicit > > operation? I do not see the point of the magic here, except _maybe_ > > for adding it to `ArrayObject` or something to allow it to work with > > the array_* functions that work without references, in which case I > > think we think this RFC is the wrong approach as it is inadequate for > > that purpose. > > I think the motivation is exactly what you said. Allowing developers more control over how the object is treated when casted to an array - which would include when it is passed into an array_* function. Here is a use-case: if(is_object($arrayOrObject)){ $a =3D array_map($callback,$arrayOrObject->toArray()); } else { $a =3D array_map($callback,$arrayOrObject); } becomes $a =3D array_map($callback,$arrayOrObject); I'm not making an argument one way or the other for whether the above is justification, but, it does at least allow the above simplification of code= . > PHP=E2=80=99s pretty magical already, so adding another magic method isn= =E2=80=99t out of > the question and would keep things inline with how some things are done > already. The idea for having magical casting is to make it simpler in the > user land for general array behavior when reading or looping. > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --=20 Chase Peeler chasepeeler@gmail.com --0000000000002ffc07059dc31bf3--