Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70130 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57153 invoked from network); 14 Nov 2013 21:13:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Nov 2013 21:13:36 -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 209.85.212.181 as permitted sender) X-PHP-List-Original-Sender: me@chrislondon.co X-Host-Fingerprint: 209.85.212.181 mail-wi0-f181.google.com Received: from [209.85.212.181] ([209.85.212.181:57923] helo=mail-wi0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 34/C0-53459-FFC35825 for ; Thu, 14 Nov 2013 16:13:36 -0500 Received: by mail-wi0-f181.google.com with SMTP id f4so87132wiw.2 for ; Thu, 14 Nov 2013 13:13:33 -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=pkfL+u+yXWIq30rZOEL/kFsxxCxw/karrfQOjrjs/GE=; b=hHCDfGKb+aVNpggZbakLK+vLKG9BDnNeVLn8L2rVjUgIuzWsE918WWST/FztNeyY0n vDGHaX2ushuvNvPwPl6DJGw0IivewH3MdBQrnYRWwt4RHmajNsmSuMXJjS32QZGoqhKc V1BU4UltBdu4Tp59sdrYM8Utya6oF83L19ib1t48Gb77V7PdsaVCb3XLvzrM8SlNEZYl hB78A+CV5MkQ2IOj5OjPF3iKnNKvWSJkZzx8xas0TvkkRoTlZyNOiIi2DDf0mgNHWvqO Pz+p67lvyk3by5d+fnKZYl2Kziz3qvIpCcwa41So4lOew7ocCEvuzl91EgLnG24gNnSL pO/g== X-Gm-Message-State: ALoCoQk85fBN3zWMAgNre/xAx57h3qDdf+DV0G6GNyhjhg7cOXrOllc++e5NXk2VJNwcIkck2YfJ MIME-Version: 1.0 X-Received: by 10.194.175.202 with SMTP id cc10mr4032368wjc.48.1384463613199; Thu, 14 Nov 2013 13:13:33 -0800 (PST) Received: by 10.216.40.199 with HTTP; Thu, 14 Nov 2013 13:13:33 -0800 (PST) X-Originating-IP: [66.219.207.210] In-Reply-To: References: Date: Thu, 14 Nov 2013 14:13:33 -0700 Message-ID: To: pierre@pcservice.co.za Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=089e013d0f48de8c1104eb298d65 Subject: Re: [PHP-DEV] Proposal: Type Casting User Classes From: me@chrislondon.co (Chris London) --089e013d0f48de8c1104eb298d65 Content-Type: text/plain; charset=ISO-8859-1 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 called 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 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. 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 */ ) 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 = AdminUser::createFromUser($user); or $admin = new AdminUser($user); but if AdminUser extends User I could see how type casting could be appropriate. 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 --089e013d0f48de8c1104eb298d65--