Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70155 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37705 invoked from network); 15 Nov 2013 11:30:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Nov 2013 11:30:45 -0000 Authentication-Results: pb1.pair.com header.from=phoenix@jonstirling.co.uk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=phoenix@jonstirling.co.uk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain jonstirling.co.uk from 209.85.214.45 cause and error) X-PHP-List-Original-Sender: phoenix@jonstirling.co.uk X-Host-Fingerprint: 209.85.214.45 mail-bk0-f45.google.com Received: from [209.85.214.45] ([209.85.214.45:55462] helo=mail-bk0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6E/12-25016-4E506825 for ; Fri, 15 Nov 2013 06:30:44 -0500 Received: by mail-bk0-f45.google.com with SMTP id r7so1604947bkg.32 for ; Fri, 15 Nov 2013 03:30:41 -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:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=J2ITgdnTOZrSot8jQbsaFe4VFBA4moAIluOGdzFwzyI=; b=PUZ/wvg6Im169d8+djG8Qgvnm7cnY/mKGJSbUSA1UOsN5xtVN91yVHvdrRzPaBghiw vBC1IJYTXeUNd/ISIzcC3JZmdkIqBGc82SPvGyayeq4KnYx/56bVrz/LnK3iyTqpylfY kLhbq37TUIxYLYkZ3ST25Uc1FQoa28l3TVfaY0txhmAa2K2vRsbXhUtTOi8beqe5N6Xz 0VRIcsZbZA8pCoAs6ZIUmzrWJCV1Es6gGLGkUvV84bSWrrcoU4GP/Fwpn7HuZzMY8BYR RUyb6BynY58MPjoSQmhmYnTBdAgB3Eb54usM/OECUJWTP9GC2zjtRu22S1QWnXb5IxvY zpGw== X-Gm-Message-State: ALoCoQkZowxrXn8WxDl9XODSIO+MLj8iwNJ81e0R5uZw6pyfB75OdFqayyXJbpL7pLogY/kyCyVC MIME-Version: 1.0 X-Received: by 10.205.10.132 with SMTP id pa4mr4266394bkb.15.1384515041301; Fri, 15 Nov 2013 03:30:41 -0800 (PST) Received: by 10.204.111.136 with HTTP; Fri, 15 Nov 2013 03:30:41 -0800 (PST) X-Originating-IP: [62.252.0.138] In-Reply-To: References: Date: Fri, 15 Nov 2013 11:30:41 +0000 Message-ID: To: Chris London Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=20cf30223b13397e1504eb358759 Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: phoenix@jonstirling.co.uk (Jonny Stirling) --20cf30223b13397e1504eb358759 Content-Type: text/plain; charset=ISO-8859-1 As a thought. Would it not be possible to, instead of implementing a new __cast method, make use of the existing __clone functionality? Effectively aren't you trying to clone one object into another just of another type? While I can't think of many use cases off the top of my head, I certainly see the benefit of this proposal and it doesn't seem to have any BC that I can see. (First post so please excuse me if I'm missing things / making a daft point.) Cheers. Jonny. On Thu, Nov 14, 2013 at 6:39 PM, Chris London wrote: > 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 > --20cf30223b13397e1504eb358759--