Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67827 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99979 invoked from network); 25 Jun 2013 20:16:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Jun 2013 20:16:32 -0000 Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 108.166.43.75 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 108.166.43.75 smtp75.ord1c.emailsrvr.com Linux 2.6 Received: from [108.166.43.75] ([108.166.43.75:56207] helo=smtp75.ord1c.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BD/53-18197-F9AF9C15 for ; Tue, 25 Jun 2013 16:16:32 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.relay.ord1c.emailsrvr.com (SMTP Server) with ESMTP id 8A7D81E80ED; Tue, 25 Jun 2013 16:16:29 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp2.relay.ord1c.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id 782D11E80C4; Tue, 25 Jun 2013 16:16:28 -0400 (EDT) Message-ID: <51C9FA9C.8050403@sugarcrm.com> Date: Tue, 25 Jun 2013 13:16:28 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: Anthony Ferrara CC: "internals@lists.php.net" References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC: Protocol Type Hinting From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > Hey all, > > I want to throw out this draft RFC to get the concept floating around and > get feedback early. > > https://wiki.php.net/rfc/protocol_type_hinting I'm not sure I understand why it is good to have this. This way of checking interfaces is very expensive (since you need to scan the function table) and relies on function names instead of explicit programmer's intent to validate the API (i.e. it assumes if two classes have method named open() this method does the same thing and the classes can be used interchangeably). Since you can easily add "implements" to your class to declare implementation of a specific protocol, why you need less safe and more computationally expensive way? Explicit declaration of interface has value way beyond mere checking function names - it is a contract that you agree to when you write "implements ThisInterface". Absent that, you code starts making assumptions that are very dangerous. Taking the example of the logger here, suppose I make my logger classes work with this API: $log->error(...), $log->info(...), $log->debug(...). Pretty standard way of doing it. This is usually done by __call. Now, when I declare that class implements Logger, I either require __call or just rely on the fact that if you said "implements Logger" then you know what "Logger" means and that it should accept the methods above and do the right thing. However, if I use "protocol typing" (please dispose with "hinting", we really should stop dragging around this unfortunate misnomer) then what would we look for in such class? Would we just accept any class? Any class that implements __call? Also this setup would be rather fragile as absent formal declaration of interface in the implementing class, it is very easy to miss changes in the interface, and since there's no explicit link between implementing class and the interface, no tool can warn you about that change in the interface until you try to use the object and it starts failing. With explicit declaration any good IDE will tell you your implementation is missing a method, and even missing that you'd be told about the class no longer being good immediately on loading, not when you try to use it. Also note that unlike the interface check, this check would also force loading the checked interface (since you can't check the methods without having it loaded) which provides additional performance drag. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227