Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70120 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41362 invoked from network); 14 Nov 2013 19:21:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Nov 2013 19:21:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=pierre@pcservice.co.za; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=pierre@pcservice.co.za; sender-id=pass Received-SPF: pass (pb1.pair.com: domain pcservice.co.za designates 67.18.144.9 as permitted sender) X-PHP-List-Original-Sender: pierre@pcservice.co.za X-Host-Fingerprint: 67.18.144.9 gateway06.websitewelcome.com Linux 2.6 Received: from [67.18.144.9] ([67.18.144.9:50217] helo=gateway06.websitewelcome.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 69/00-40965-DB225825 for ; Thu, 14 Nov 2013 14:21:35 -0500 Received: by gateway06.websitewelcome.com (Postfix, from userid 5007) id AFE4AE2675C64; Thu, 14 Nov 2013 13:21:30 -0600 (CST) Received: from vinacomin.websitewelcome.com (vinacomin.websitewelcome.com [50.97.101.199]) by gateway06.websitewelcome.com (Postfix) with ESMTP id 795A1E2675B84 for ; Thu, 14 Nov 2013 13:21:30 -0600 (CST) Received: from [209.85.215.53] (port=65211 helo=mail-la0-f53.google.com) by vinacomin.websitewelcome.com with esmtpsa (TLSv1:RC4-SHA:128) (Exim 4.80) (envelope-from ) id 1Vh2Tq-0005hh-7d for internals@lists.php.net; Thu, 14 Nov 2013 13:21:30 -0600 Received: by mail-la0-f53.google.com with SMTP id ea20so1945077lab.40 for ; Thu, 14 Nov 2013 11:21:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=qaLGZ51zmOCZYGTzNH9PirYgpxPCdErL+gXkhwzeyrg=; b=k45tjNWPF2v34An17RSRJ2afvw48mLY5J3IG8WIcinVoaan7vd2CRgEpy09JepC4OB tCbwsFVEGLLAQZ8E6sd4PaqZbcNVgV+w9A7JSOFncsMj3vq14/G8SNqqRm9pZZNqKFLe A4+56icojtoUvdVPU0ZEX0XichExvCe75iLcUlg3YaX9s7wCq1tgW5bztZaEAXwHwlmn OqsKIKOid3VxGO2qjy9ATqul0410+67AkjcvDfAOEeylYZnadvWys8yAWn8r558bTG99 jnrB6bC3QL7KiWoNB3EZRvvc9rsRSoNIFMzzow9lPRJA6THPM2N5dRKhbBH0JwbakmBW ZKpA== X-Received: by 10.152.19.74 with SMTP id c10mr1781331lae.48.1384456887816; Thu, 14 Nov 2013 11:21:27 -0800 (PST) MIME-Version: 1.0 Reply-To: pierre@pcservice.co.za Received: by 10.112.33.233 with HTTP; Thu, 14 Nov 2013 11:20:57 -0800 (PST) In-Reply-To: References: Date: Thu, 14 Nov 2013 21:20:57 +0200 Message-ID: To: Chris London Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e01493fac01596404eb27fd6c X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vinacomin.websitewelcome.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pcservice.co.za X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: (mail-la0-f53.google.com) [209.85.215.53]:65211 X-Source-Auth: pierre@pcservice.co.za X-Email-Count: 2 X-Source-Cap: cGNzZXJ2aWM7cGllcnJlO3ZpbmFjb21pbi53ZWJzaXRld2VsY29tZS5jb20= Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: pierre@pcservice.co.za (Pierre du Plessis) --089e01493fac01596404eb27fd6c Content-Type: text/plain; charset=ISO-8859-1 On Thu, Nov 14, 2013 at 8: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 > Hi Do you have any use cases why this would be useful? IMO an object is only meant to do what it is created for. Converting objects to different types can have very unexpected results. If you want to reuse code, you can just use traits, or you can use dependency injection to get functionality of one object inside another. Also what would happen if you cast objects to invalid types? E.G if you convert a person entity to a product entity, which doesn't have the same properties or methods? --089e01493fac01596404eb27fd6c--