Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105598 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56615 invoked from network); 6 May 2019 17:33:49 -0000 Received: from unknown (HELO mail-it1-f195.google.com) (209.85.166.195) by pb1.pair.com with SMTP; 6 May 2019 17:33:49 -0000 Received: by mail-it1-f195.google.com with SMTP id q14so20652146itk.0 for ; Mon, 06 May 2019 07:37:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=9le+kN6E9PU3D88qe/tk3a3nuR30A3k0ODNkYEI/GsM=; b=arU0MCpKE5x+dn3AwIG4AiwI+uh7H9FJyqE12eMRKEPbv5zhnW8VBrTN5EzFQipfAq Zeo+IoKYbCqSkhfOOI5p7T93DH7nkHTHvwfRPbSMm0Jsm6YeIEPhLZf7FUsB88nz3L3c fiGyLeLeIjXLwP2YWnMr1QMQepVbsYkeAAHOTKb2MqBAtwdwhGy4A7NUvqn1CBdSOQos /flVEGw1YuxJsUpZ8zPUAisaB98Zx24eZSqSNttpfc8zajROETJIbFrPQbMJ1CSWHbgI lp3fuX7XFjFcRgI83jNFvOjYNbb4NbjffvGVTu2wXN6Wqd/iFOcv7ToX70KY8YZQ8oWp 87cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=9le+kN6E9PU3D88qe/tk3a3nuR30A3k0ODNkYEI/GsM=; b=MXjW7V+LfWKnEOG+lkQnur6jWis0BdGwcUH2MBhfjXlJJPhx6XZzrRIekWVbg0SQzi aiWveR7fsoBFn5TklGj13DpiXry0j2R79FsmgQOdqT1bmZSjASmF4bpXfY5RcU219tsR dqcyFtm1UGAKDQrdTMxnubgD/6s5OFudPWyCW4hSsbBG106haqInS2W4MHvcKGilOE+n ShT7KdvYDEWid+BScHXVU9ZFO7McnItEM9hvR5EYAvTjiDcR3P59XC4FJB2Ccra0LhN1 a0SIRzRULKMJVASMKOxqP6+8rzMBk/JYXIeBGFLxon8zjaVct48rueFTGnY0YluZ2d1M Ch5g== X-Gm-Message-State: APjAAAXx3hIGVmykqrRm5zA+MOiX8Jiu6+JdWdRcqMUnRBYPdGWg8HFE RlxFWRJpXG3ZduKzMdy2q/M= X-Google-Smtp-Source: APXvYqxlLAxkTJ81OLIHKmNMT1VRks41EscqIgReg4uXrlGWLkhcS2KK3E7TwQNI+p1NH11uu6Qzxw== X-Received: by 2002:a24:4ace:: with SMTP id k197mr19002223itb.34.1557153448207; Mon, 06 May 2019 07:37:28 -0700 (PDT) Received: from [10.137.43.10] ([205.185.214.246]) by smtp.gmail.com with ESMTPSA id 138sm1419777ith.18.2019.05.06.07.37.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2019 07:37:27 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) In-Reply-To: <384B1934-FC7A-414D-BC31-E5521B2F0E17@benramsey.com> Date: Mon, 6 May 2019 10:37:24 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <27CB5952-9688-44BF-B401-F3E04DE00710@gmail.com> References: <384B1934-FC7A-414D-BC31-E5521B2F0E17@benramsey.com> To: Ben Ramsey X-Mailer: Apple Mail (2.3445.104.8) Subject: Re: [PHP-DEV] Proposal for a RFC From: stevenwadejr@gmail.com (Steven Wade) > On May 5, 2019, at 10:59 AM, Ben Ramsey wrote: >=20 >=20 >> On May 4, 2019, at 09:58, Steven Wade wrote: >>=20 >> Hi Internals team! >>=20 >> I have an idea for a feature that I'd love to see in the language one = day and wanted to run the idea by you all.=20 >>=20 >> The idea is to add a new magic method "__toArray()" that would allow = a developer to specifiy how a class is cast to an array. The idea is the = same mentality of __toString(), but, for arrays. >>=20 >> I would personally love this feature and those I've run it by were = also excited by the idea. So I'm soliciting feedback in hopes that = things go well and I can officially write the RFC. As for = implementation, Sara Golemon is awesome and while chatting a few months = back, knocked out a proof-of-concept implementation = . = There's still work to be done if this proposal gets to the RFC phase, = but again, just gauging interest here. >>=20 >> I appreciate any feedback you all can provide. >>=20 >> Thanks, >>=20 >> - Steven Wade >=20 > Using existing language functionality, would it satisfy the similar = needs if you implement IteratorAggregate and return an ArrayIterator = from the getIterator() method? Obviously, the (array) cast wouldn=E2=80=99= t use this, but you could do $object->getIterator()->getArrayCopy(). >=20 > I=E2=80=99m not against the proposal. I=E2=80=99m mainly asking if = there are other ways in the language today to accomplish similar things. = I would prefer to see interfaces used over more magic methods (something = like ArraySerializable). PHP already has the magic built in. The proposal is really just a way to = control the magic. We already have "(string) $foo" and "__toString()", = so the idea of implementing a "__toArray()" method is bringing a feature = more inline with what's already there.=20 I think it reads easier and cleaner "(array) $foo", and a simpler as a = developer to implement than a mix of chained methods and interfaces. That being said, adding an interface like `ArraySerializable` might be = nice too. I'm seeing technical arguments against adding magic casts, but = (just spit-balling here), what if a class implemented a new = `ArraySerializable` interace with whatever method it demands, and if = that is attempted to be cast to an array either manually by calling = "(array) $foo" or PHP attempting to, then the array serialize method is = called. =C2=AF\_(=E3=83=84)_/=C2=AF