Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70119 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38541 invoked from network); 14 Nov 2013 18:39:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Nov 2013 18:39:53 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@chrislondon.co; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@chrislondon.co; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain chrislondon.co does not designate 209.85.212.181 as permitted sender) X-PHP-List-Original-Sender: me@chrislondon.co X-Host-Fingerprint: 209.85.212.181 mail-wi0-f181.google.com Received: from [209.85.212.181] ([209.85.212.181:60579] helo=mail-wi0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/00-38416-8F815825 for ; Thu, 14 Nov 2013 13:39:52 -0500 Received: by mail-wi0-f181.google.com with SMTP id f4so3120151wiw.14 for ; Thu, 14 Nov 2013 10:39:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=Rbqos9XkD8GhL1cym4bwsBmutWmNKscPiSms5P3hOL4=; b=SOnvoxtUuLvYQ8+YFmIUFpGQOpVV2fibZUiRTgyvll5IAUzmdnDlM10zhZDDJyyCOs JQa5cxJnF1UIbGhks7Auo/A/JmGV2/9tN54GGu8ArsyCs9iHcSeXji9AX3kUg/+0iapF NU3XE/gEzQ1lx5OEfzg0peYeRvQrjwulHOxnfA+MwLplnrmMHPJ+YTv60S/thnOFc8Az yJi6i8bhIACo33c/veDq/UkPrKbgBV2WVnSVC0MGYRWbr09IOuuYbY4f8upMJnBU61fn tNvKM4lwkGxQ2kXsfX3UueAuEvLTx094JJs1ERuv9c2YDvuyNzOylAfLPjNM0zm8L8kv 8AMA== X-Gm-Message-State: ALoCoQmWo0KnRgKMou+FYvYhW/Bkj8f5vO9FUorIPloX+Z0S+esQgulFwdYDGe/uetY/+iw95KHh MIME-Version: 1.0 X-Received: by 10.194.175.66 with SMTP id by2mr3243611wjc.59.1384454388908; Thu, 14 Nov 2013 10:39:48 -0800 (PST) Received: by 10.216.40.199 with HTTP; Thu, 14 Nov 2013 10:39:48 -0800 (PST) X-Originating-IP: [66.219.207.210] Date: Thu, 14 Nov 2013 11:39:48 -0700 Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e013d19f80f1f9f04eb2768e9 Subject: Proposal: Type Casting User Classes From: me@chrislondon.co (Chris London) --089e013d19f80f1f9f04eb2768e9 Content-Type: text/plain; charset=ISO-8859-1 Hey everyone, I want to get a feel for something I would like to implement into PHP core. I would like to be able to cast objects to my own class types. Similar to already available PHP syntax: $foo = (string) $bar; The syntax would look like this: $foo = (\My\FooObject) $bar; It would also support: $foo = ( (\My\FooObject) $bar )->myFooObjectFunction(); I'm still trying to decide what would happen internally and I would like some feedback. I'm thinking of 3 different things it could do: 1) Directly change the class type, functionally equivalent to: function castType($object, $type) { return unserialize(preg_replace("/^O:[0-9]+:\"[^\"]+\":/i", "O:".strlen($type).":\"$type\":", serialize($object))); } 2) Throw a fatal error if the object doesn't match the type trying to be cast. 3) Add a new static magic method called __cast() that would allow the developer to decide what happens. Like this: namespace \My; class FooObject { public static function __cast($foo) { // Do stuff here return new FooObject; } } I'm willing to implement this myself if everybody else likes this idea. Thanks! Chris London --089e013d19f80f1f9f04eb2768e9--