Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70156 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45892 invoked from network); 15 Nov 2013 14:29:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Nov 2013 14:29:42 -0000 Authentication-Results: pb1.pair.com header.from=leverton@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=leverton@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.50 as permitted sender) X-PHP-List-Original-Sender: leverton@gmail.com X-Host-Fingerprint: 209.85.219.50 mail-oa0-f50.google.com Received: from [209.85.219.50] ([209.85.219.50:63178] helo=mail-oa0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E5/20-43531-5DF26825 for ; Fri, 15 Nov 2013 09:29:41 -0500 Received: by mail-oa0-f50.google.com with SMTP id k1so4042434oag.9 for ; Fri, 15 Nov 2013 06:29:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=foAbL66I38n1fpXVDJra2IZS8iHXjImZqe26+RIocNE=; b=KtVemjIHKISQ3zDkTz6gRNUgvzPdyt/f47v6ih96896mLiLfhEXJDgjadE9mfygTo2 /qg9wrxRGHpkO98R5PZWTbhIA+xjBMAF8qcpCLptLFMtYQdk2MDiUreZmiQA1aJxJifl MIVJwk2Vs52v7w6CGkpZFhO+Wdh9QohLme149apYYtEUtMmXguNWKYvKtVuGSEQfl6ia z8wyWqYpI6UIVCjV5CQCjGyp4IgmTkO2lpd4ur1AbpVM33wosCSTuNM+EWv1pjY7Mt9R wspkW65ktNNZWF/4/sE90VJL/gjLx+w1OBnybfg92C9CY+cvY9OtRNWzlK8Hu1MXkQsq r6lA== MIME-Version: 1.0 X-Received: by 10.60.145.136 with SMTP id su8mr7089957oeb.17.1384525778597; Fri, 15 Nov 2013 06:29:38 -0800 (PST) Received: by 10.182.124.134 with HTTP; Fri, 15 Nov 2013 06:29:38 -0800 (PST) In-Reply-To: References: Date: Fri, 15 Nov 2013 08:29:38 -0600 Message-ID: To: Chris London Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: leverton@gmail.com (Matthew Leverton) On Thu, Nov 14, 2013 at 12: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; > I've thought about this feature before in the context of PHP because I do think it would be useful, but I'm not sure if there's really a way to implement it that is any better than userland solutions. Your proposed __cast() call means that every object needs to know about every object which breaks down when multiple projects are involved. (Sure, it solves some cases nicely.) And I wouldn't be a fan of implicit conversions like: function foo (Foo $foo) { } foo(new Bar()); Assuming Bar does not descend from Foo, I wouldn't want a cast to Foo to be implicitly performed even if Bar had an appropriate "cast" method. So ultimately, I don't really see how a PHP solution is any better than just using home made explicit casts: $bar = cast_foo_to_bar($foo); or $bar = $foo->castToBar(); So I think the idea has merit, but I don't know that there's a good way to implement it. -- Matthew Leverton