Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62136 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63976 invoked from network); 14 Aug 2012 07:46:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2012 07:46:39 -0000 Authentication-Results: pb1.pair.com header.from=sv_forums@fmethod.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=sv_forums@fmethod.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fmethod.com from 209.85.215.170 cause and error) X-PHP-List-Original-Sender: sv_forums@fmethod.com X-Host-Fingerprint: 209.85.215.170 mail-ey0-f170.google.com Received: from [209.85.215.170] ([209.85.215.170:57425] helo=mail-ey0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D1/52-00812-E520A205 for ; Tue, 14 Aug 2012 03:46:38 -0400 Received: by eaao11 with SMTP id o11so23989eaa.29 for ; Tue, 14 Aug 2012 00:46:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:from:to:subject:date:mime-version:content-type :x-priority:x-msmail-priority:x-mailer:x-mimeole:x-gm-message-state; bh=E7fIukSl6eeBh4zEeXmRH0qG6JMUyhQxIoZoCO+sJNs=; b=Sx52AAHdbMltN2FMRy2Q+kae49Jp0O3gZKQsK/+3RdW6oNDH5HxmSIvxawvb7PkDOr jweVtZZUIWKxoyziUve2MNwCE2WOh4tz7WThfHWuG51q1nZIaSNzmKbpwKfzNJ44zzuh UIpS1tyZqamnRTTHNZEXoU+FppB7a1iL0VsZV5mJJgaJWClgSHQJBz78qL15NzTbRH95 RuTKI7+mC/OyWDmVmrKntXxUJuIc0G6Sub8gKYcs2c3Z3+wXTSO0W8ISDpN+yCVfGs9J U0iiN6vCIzFone95+Z59QFzro5DjOUh6QRYsB5lNWJ4G2oVt+qNv0rnpzR5nGPOuF3ED HrAQ== Received: by 10.14.218.134 with SMTP id k6mr13856176eep.14.1344930394740; Tue, 14 Aug 2012 00:46:34 -0700 (PDT) Received: from pc (95-42-66-13.btc-net.bg. [95.42.66.13]) by mx.google.com with ESMTPS id a7sm4406956eep.14.2012.08.14.00.46.31 (version=SSLv3 cipher=OTHER); Tue, 14 Aug 2012 00:46:33 -0700 (PDT) Message-ID: To: Date: Tue, 14 Aug 2012 10:46:26 +0300 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_000F_01CD7A0A.0DBCA1A0" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.6157 X-Gm-Message-State: ALoCoQlWqMs1lzjSN+8AXa/7QeFUrxv2KNjrFZaBES79draTMc1RuMJ/Ss7XTji6G/gWYnNQWPQl Subject: Inline typecasting / typehinting for classes and interfaces From: sv_forums@fmethod.com ("Stan Vass") ------=_NextPart_000_000F_01CD7A0A.0DBCA1A0 Content-Type: text/plain; charset="windows-1251" Content-Transfer-Encoding: quoted-printable I've felt the need for this for some time. Proposed syntax: ------------------------- $x =3D (InterfaceName) $container->service; Proposed behavior: --------------------------- Checks if the instance if an instance of the given class/interfaces. If = yes, does nothing, if not, issues a fatal error. Pseudo code: $temp =3D $container->service; if ($temp instanceof InterfaceName) { $x =3D $temp; } else { trigger_error('The object can not be cast to InterfaceName'); } Use cases: ---------------- As my example hints, the intent is to provide type safety and hinting = for the increasingly used dependency injection container pattern.=20 It's useful to provide type safety also for all similar patterns which = may not return a result of the expected type: Registry, ServiceLocator, = Factory etc. Additionally to runtime type safety, this will also serve to = editors/IDEs as an inline hint to provide proper autocompletion, i.e. = instead of this: /** @var $foo InterfaceName */ $foo =3D $container->service; People can now just write: $foo =3D (InterfaceName) $container->service; Alternative syntax: -------------------------- I realize "casting" isn't a perfect match as the behavior is equivalent = to inline type hinting. On the other hand, casting and inline type = hinting in dynamic languages often behave identically (see the casting = behavior in ActionScript which combines type hints with a JS-like = dynamic type system). And this syntax doesn't introduce new keywords. Still, alternative ways = to implement this could be: 1. $foo =3D $container->service as InterfaceName; 2. $foo =3D $container->service is InterfaceName; 3. $foo =3D (InterfaceName $container->service); 4. InterfaceName $foo =3D $container->service; All of those would be fine, especially 4, which has precedent in other = languages, and in PHP as well with typehinting and in catch blocks: = catch (ClassName $e) { ... } I'm ok with suggestions for any syntax that is as short, if it fits the = behavior better. Feedback? Stan ------=_NextPart_000_000F_01CD7A0A.0DBCA1A0--