Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70146 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94667 invoked from network); 15 Nov 2013 01:37:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Nov 2013 01:37:12 -0000 Authentication-Results: pb1.pair.com header.from=will@fitch.so; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=will@fitch.so; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain fitch.so designates 66.111.4.27 as permitted sender) X-PHP-List-Original-Sender: will@fitch.so X-Host-Fingerprint: 66.111.4.27 out3-smtp.messagingengine.com Received: from [66.111.4.27] ([66.111.4.27:53048] helo=out3-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/10-27935-6CA75825 for ; Thu, 14 Nov 2013 20:37:10 -0500 Received: from compute2.internal (compute2.nyi.mail.srv.osa [10.202.2.42]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id A47C020AEC; Thu, 14 Nov 2013 20:37:07 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute2.internal (MEProxy); Thu, 14 Nov 2013 20:37:07 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fitch.so; h= content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=mesmtp; bh=VTJ06UdejVPFw4QnTdr5ZCGdTMw=; b=Hb+F8TBoFFnTgmPpH2zt9bWcbcXI sTWnLVGszITIKXyvYlesrGtCh0HuYvFRojbuUDjPNtthblB1pVBmJH2hg94GiNfi FBEpz6CSN4uTiMEcD/TN0hifqbxh5TNuIhI5Cm7xtmCbJqQ9ZDxb9c4RoPb0lFal TO70RXAUDkMao0I= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-type:mime-version:subject:from :in-reply-to:date:cc:content-transfer-encoding:message-id :references:to; s=smtpout; bh=VTJ06UdejVPFw4QnTdr5ZCGdTMw=; b=BA euhXiIaAjbQYVCA1uCHBiJNeuzZieB0D7RPwV8ITG6MERu3iYiS+TNIvc2E3coDv 50nsFexd3JMXWOU2iAzE27dP5KAHYrjutS68HjxHnWsZtgovOtQle0s/vWffH8j6 xDnzA+IXImXNbzDzijfM1SHh0EpraGhDFOeh8Utrc= X-Sasl-enc: h+7qkMj3iCrymjs3cA81WLz9gvnL610yzi8Fl2UlMlxO 1384479427 Received: from williams-mbp-2.home (unknown [98.114.234.219]) by mail.messagingengine.com (Postfix) with ESMTPA id F1B4A6800A1; Thu, 14 Nov 2013 20:37:06 -0500 (EST) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.0 \(1822\)) In-Reply-To: Date: Thu, 14 Nov 2013 20:37:06 -0500 Cc: pierre@pcservice.co.za, "internals@lists.php.net" Content-Transfer-Encoding: quoted-printable Message-ID: <8B0BFAD3-A573-431B-AA08-312108378EE5@fitch.so> References: To: Chris London X-Mailer: Apple Mail (2.1822) Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: will@fitch.so (William Fitch) On Nov 14, 2013, at 4:13 PM, Chris London wrote: > On Thu, Nov 14, 2013 at 12:20 PM, Pierre du Plessis > wrote: >=20 >>=20 >> Do you have any use cases why this would be useful? >>=20 >=20 > Basically the reason I want it is to make sure the function I just = called > returned the object I expected. Kind of like how we can specify what = object > we're expecting for parameters like this: >=20 > function foo (User $user) { > // do stuff here > } >=20 > I want to be able to make sure the object I have is the right type. In = this > case, I wouldn't want it to actually change the object type but = instead > through an exception I could catch. (obviously, there are ways to do = that > now by writing my own functions and throwing my own exceptions). The > company I'm working for right now uses Zend Framework 2 and in Zend > Framework 2 they have factories which could return just about anything = and > I want to make sure I got the right object. I=92m confused - on one hand, you=92re referring to the already existing = type hinting of parameters. On the other, you=92re making the case for = instanceof. =20 if ($someZendObject instanceof What\I\Expect) This really isn=92t a use case for object type casting. =20 >=20 > Another minor use case would be that the IDE would be able to do > function/parameter hints. (I do know some IDEs will let you do /* = @var > $foo User */ ) If you=92re using a relatively modern IDE and using parameter type = hinting, this already exists. >=20 > Another situation we ran across is we're using Propel and we would = pull > database records that Propel turns into User objects. If some of those > users are admins then we would want them to be AdminUser objects. = Obviously > you could write work arounds like : >=20 > $admin =3D AdminUser::createFromUser($user); >=20 > or >=20 > $admin =3D new AdminUser($user); >=20 > but if AdminUser extends User I could see how type casting could be > appropriate. There are two types of object type casts: upcast and downcast. Upcast = is when you have a class that refers to an object that inherits from a = root class. Downcast is casting an object from the root towards a child = or subclass. What you=92re referring to is downcasting. (User -> = AdminUser). This is the only valid use case provided thus far, but even = then, there are other methodological approaches that can be taken to = mitigate this scenario. >=20 > Also what would happen if you cast objects to invalid types? >>=20 > E.G if you convert a person entity to a product entity, which doesn't = have >> the same properties or methods? >>=20 >=20 > I imagine throwing an exception would be appropriate >=20 > Thanks! > Chris London