Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70147 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96963 invoked from network); 15 Nov 2013 02:17:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Nov 2013 02:17:23 -0000 Authentication-Results: pb1.pair.com header.from=me@chrislondon.co; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@chrislondon.co; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain chrislondon.co does not designate 74.125.82.174 as permitted sender) X-PHP-List-Original-Sender: me@chrislondon.co X-Host-Fingerprint: 74.125.82.174 mail-we0-f174.google.com Received: from [74.125.82.174] ([74.125.82.174:60621] helo=mail-we0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 91/80-27935-13485825 for ; Thu, 14 Nov 2013 21:17:22 -0500 Received: by mail-we0-f174.google.com with SMTP id t61so3004647wes.33 for ; Thu, 14 Nov 2013 18:17:19 -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=+CbqMFCvPRPBUfsE5RurleuDneQRwidy/DXgNuM/dxo=; b=TFB26Pge1c4/gzBJwUQTzLxfrSN87Yf4VC2WSWtpmDBbYnDs5KVNlZETqzTdLVTxuR jTaUh1kB1W9V7jPY2v9o554cmdWVzc0cI+yj1HpfiiM5BF+3iAGrVcZqYZ/feMxDkoHW pI2kinFp/UTvV3r7R5otpnQ2bBkN8evokdUWXcQYVhwVybZ2+t8bHRW8IUfI1Q4JzCNJ BlXjqlWFwvC8Fh/6kSDrQkdPTb1WkOo1Q8469QGORNI86TpxqH+/xbV5OiWH2yaOY9Rv SelSJbHvf7r1Ivqfy/5/gCaTbQ4+bZ1ja4wmIQriZrHidp8Dwdb7BU/aY26tdksWboU2 G0ug== X-Gm-Message-State: ALoCoQm2p82n1WWFhMKonbwjKz/CycpiT01Pd0rm+VSaFVGaJquhJDptDRyAPx7mTEoZOcPrvE3D MIME-Version: 1.0 X-Received: by 10.180.73.6 with SMTP id h6mr5741274wiv.1.1384481839027; Thu, 14 Nov 2013 18:17:19 -0800 (PST) Received: by 10.216.40.199 with HTTP; Thu, 14 Nov 2013 18:17:18 -0800 (PST) X-Originating-IP: [66.219.207.210] In-Reply-To: <8B0BFAD3-A573-431B-AA08-312108378EE5@fitch.so> References: <8B0BFAD3-A573-431B-AA08-312108378EE5@fitch.so> Date: Thu, 14 Nov 2013 19:17:18 -0700 Message-ID: To: William Fitch Cc: pierre@pcservice.co.za, "internals@lists.php.net" Content-Type: multipart/alternative; boundary=f46d043c7f0436b27804eb2dccf0 Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: me@chrislondon.co (Chris London) --f46d043c7f0436b27804eb2dccf0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Thu, Nov 14, 2013 at 6:37 PM, William Fitch wrote: > > On Nov 14, 2013, at 4:13 PM, Chris London wrote: > > > On Thu, Nov 14, 2013 at 12:20 PM, Pierre du Plessis > > wrote: > > > >> > >> Do you have any use cases why this would be useful? > >> > > > > Basically the reason I want it is to make sure the function I just call= ed > > returned the object I expected. Kind of like how we can specify what > object > > we're expecting for parameters like this: > > > > function foo (User $user) { > > // do stuff here > > } > > > > 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 th= at > > 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 instanc= eof. > My reference to already existing functionality was to try and prove there's a need for type casting. > > if ($someZendObject instanceof What\I\Expect) > > This really isn=92t a use case for object type casting. > If we implemented type casting we could reduce the following: $factory =3D $this->getServiceManger()->get('myObject'); if ($factory instance of \Parent\Of\What\I\Expect) { $factory =3D \What\I\Expect::convertFromParentOfWhatIExpect($factory); } elseif (!$factory instance of \What\I\Expect) { throw new Exception('Invalid factory return'); } to $factory =3D (\What\I\Expect) $this->getServiceManger()->get('myObject'); I think the one line is cleaner than having an "if instanceof" after every factory request. > > > > 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 hintin= g, > this already exists. Yes, but only if the function returns a single type. > > > > 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 : > > > > $admin =3D AdminUser::createFromUser($user); > > > > or > > > > $admin =3D new AdminUser($user); > > > > 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. > I actually think it should also work for upcasting if you follow the Liskov Substitution Principle. To me, since PHP already has implemented type casting to (int), (string), (bool), etc. the value in type casting is already there. I just want to take it to the next level. > > > > > 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? > >> > > > > I imagine throwing an exception would be appropriate > > > > Thanks! > > Chris London > > --f46d043c7f0436b27804eb2dccf0--