Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97246 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 66629 invoked from network); 2 Dec 2016 13:37:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Dec 2016 13:37:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=bowersbros@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bowersbros@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.45 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 74.125.82.45 mail-wm0-f45.google.com Received: from [74.125.82.45] ([74.125.82.45:36415] helo=mail-wm0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4B/07-01781-41971485 for ; Fri, 02 Dec 2016 08:37:24 -0500 Received: by mail-wm0-f45.google.com with SMTP id g23so17094521wme.1 for ; Fri, 02 Dec 2016 05:37:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=jdWrroD+2HDrFj+5gkp/CWmYjfw/WpcvBe77KzrF4dA=; b=HMtz1xrWPEabybOEbSwRQPqU9LB9TSEQ2r32kgkiAYmeVitC8MIGd1HvuCaJkiL4Ym Yj0/KFP/CNTqY7IR51lTkvm+zTeOfVSJpbuF+0A+UG+nAC0i42RsT7pOkSRjVByBpfpD eceReUEQLVxGzZD9RW6K6qsXNH8S5frKznTj4wPKAzMV4MnV0TQNqS7WVTaxPwod2fSk VSLheGw6SNGBVstiFHdIfGT261wa+nDMbZ1/M7gmYEegBkr9SWK/QgBL4PA+pccWQIPY EiJCGVFzVo/3iEmO8BLN7vH/C0s/78nD0C23HYglsIdbJDGLwMmpEE81Q9f4FlvmF1bX TX7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=jdWrroD+2HDrFj+5gkp/CWmYjfw/WpcvBe77KzrF4dA=; b=P/Vv7ZGzOVA5gIoRi7Vm5At4OpuAF2cslekaq3DATR1wdeb8uS5IQjYX8SX0zpsF0P DF6DqVSs6V5PkGSWZ6Gzvm6FTDF/EhHWPtf4Sxy5XwaJsuVYhBPbA6NeA5HJIGnrOuTU XQYPi6PrQzBKXZEFS37WTMQbkuFIKkAk1iNTpxTDQTFFB7Am/LpNCSMHoq6g392p57kJ k1z72cfCbAkAtjxw9xNQYpR+5NOGoUEoe19sCsNhOh9lmaqUGsNTkmrGeiQATQGbLNFf ++bzNqBW+mC+Ki2+z7Vm/eG9zhilD/zjuMvlSTFoOQHyoh6ZinH92+eIFk185XtsNaEK LOqw== X-Gm-Message-State: AKaTC030JGwlLxLZ+j4EO5bhLQLwMc+fjcbicVrvm4Of1lGgfR2dVso2y3z/x5Ip+YNvNv9VkDV5IZUC622jOQ== X-Received: by 10.28.35.205 with SMTP id j196mr3116669wmj.62.1480685841301; Fri, 02 Dec 2016 05:37:21 -0800 (PST) MIME-Version: 1.0 Received: by 10.28.232.77 with HTTP; Fri, 2 Dec 2016 05:37:20 -0800 (PST) Date: Fri, 2 Dec 2016 13:37:20 +0000 Message-ID: To: PHP Content-Type: multipart/alternative; boundary=001a113ea85698588a0542ad0965 Subject: [Concept] Magic Casting From: bowersbros@gmail.com (Alex Bowers) --001a113ea85698588a0542ad0965 Content-Type: text/plain; charset=UTF-8 Hello All, In PHP we currently have the ability to type hint classes in method signatures, however, sometimes it would be useful to convert the items to a different instance / type. An example is collections and arrays. If implemented properly, an array and a collection could be used interchangeably, although a collection may be preferred. For consistency, having the ability to dynamically cast from an array to a collection would be very useful. An idea of how this may look would be: items = $items; } ... public function __cast(array $items) : Collection { return new static($items) } } -- The __cast method MUST return an instance of itself, so that when called like so: function convert_me(Collection $collection) { var_dump($collection); } convert_me([1,2,3]); the result there would be a Collection instance, instead of an array. If the type passed is not accepted by __cast() or __cast() throws an exception, then the current errors of invalid type are thrown, or perhaps the custom exception message. I'm not sure if this has been proposed before, and if it has, please could someone assist in me finding it, I couldn't find it by a quick search on the wiki.php.net/rfc page. Thanks. Alex. --001a113ea85698588a0542ad0965--