Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62146 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16571 invoked from network); 14 Aug 2012 15:45:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Aug 2012 15:45:39 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wg0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:32995] helo=mail-wg0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F7/3C-00812-2A27A205 for ; Tue, 14 Aug 2012 11:45:38 -0400 Received: by wgx1 with SMTP id 1so400324wgx.11 for ; Tue, 14 Aug 2012 08:45:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=GsyGPHi8nmQiMwyTl4OuppVpPEpB+Ht4ot5SyyN+LLA=; b=C4eFUuMmBMu/1BFUZpBm1g+qm2LWTyVLmloVYG+u0Ah8zXOa3pxvHDOXoQenfuEIXR j2ZvdXmYDUgNDmz4zY0mNpGCG3mfyNg8+ptL0sVWh0Sl9Ox1mTqsCtlMMsm5sBbX+iwK 245GWr+cD+kDfHFPZ4sHMwrVyI08OnWnJyHkQtTqKNZGkbFdpedZ2xRMBn7/JuV5fzPj 1GUcllti0fPsEXFtM+WB5o7HsgrJv/GLaH8/nOD6jtPdQ3SVq68n+uB5fEknoZ+WFv5v QlTz1OG02kWDzHgvJgxtujPK3C0F7V8dssTx9GL7cqNfOHdMiDOxgqA3vCbxHB3E0ufW 4Kjg== MIME-Version: 1.0 Received: by 10.180.86.106 with SMTP id o10mr28872382wiz.22.1344959135161; Tue, 14 Aug 2012 08:45:35 -0700 (PDT) Received: by 10.180.103.195 with HTTP; Tue, 14 Aug 2012 08:45:35 -0700 (PDT) In-Reply-To: References: Date: Tue, 14 Aug 2012 11:45:35 -0400 Message-ID: To: Levi Morrison Cc: Stan Vass , internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d041827527d1e3604c73bb3cb Subject: Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces From: ircmaxell@gmail.com (Anthony Ferrara) --f46d041827527d1e3604c73bb3cb Content-Type: text/plain; charset=ISO-8859-1 Levi, On Tue, Aug 14, 2012 at 9:51 AM, Levi Morrison wrote: > On Tue, Aug 14, 2012 at 1:46 AM, Stan Vass wrote: > > I've felt the need for this for some time. > > > > Proposed syntax: > > ------------------------- > > > > $x = (InterfaceName) $container->service; > > I'm against this. Let's be honest, how different is this that an > optionally static type? > > InterfaceName $x = $container->service; > > To be clear, I WANT optionally static typing. For the most part, > type-hinting in class methods solves the static typing needs, but if > we were able to declare a class member to be a certain type that would > be another significant improvement. I have a proposal in the works, > but I don't want to hijack your thread. > > To sum up: I think we need something LIKE what you are proposing, but > I'm against this particular proposal. > I agree with you. The one case where this syntax may be very useful is if we want to implement class casting. So introduce a pair of magic methods public function __castToClass($class) { } public static function __castFromScalar($scalar) { } Then, $foo = (A) $objectExtendingFromA; // No-op $foo = (A) $objectNotFromA; // Calls $objectNotFromA->__castToClass('A'), and if it returns something that matches A, good, if not fail $foo = (A) 1; // Calls A::__castFromScalar(1); Now, note that it would only work on class names, as doing it on interfaces would only work in the objectNotFromA case (and even then, it would be weird). Now, I'm just saying that this is the only way I'd want to see something like this. The use cases for it are there, but extremely narrow and likely better solved with other mechanisms. So I'm not completely sure I like the concept as a whole, but just throwing it out there... Anthony --f46d041827527d1e3604c73bb3cb--