Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67863 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13273 invoked from network); 26 Jun 2013 12:35:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jun 2013 12:35:41 -0000 Authentication-Results: pb1.pair.com header.from=maxpower57@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=maxpower57@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.178 as permitted sender) X-PHP-List-Original-Sender: maxpower57@gmail.com X-Host-Fingerprint: 209.85.212.178 mail-wi0-f178.google.com Received: from [209.85.212.178] ([209.85.212.178:35781] helo=mail-wi0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/B5-18025-C10EAC15 for ; Wed, 26 Jun 2013 08:35:40 -0400 Received: by mail-wi0-f178.google.com with SMTP id k10so1812171wiv.5 for ; Wed, 26 Jun 2013 05:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=9baQPukoiORkck03UhASwl3R+EuXSgFzyjmMKC3Lm+s=; b=GZsMaUf+kbqZmZNrfw/1TQ2zOaHHgwLvevCROfRdaVSgXk5WbMb5r7U57nTeSAnXhz p3CSdiJ/jERiMAC+0I+xPv9blGuKn/+Fb4pvkYeM8PXExOa9P78PDVSOEZlySMr2HfgQ qA6alkKMmAOtSYiHG1NFfmzO9sXwuaKyc/fPsZdckLMpKNvwxdcvueX8NmlJFnXv8uQx hIWfMjhTBdCOykyWOd2uhTdHyOAhbq7OG+wskepWwxmJ67L70u3cWjGApVbTno9KxWMp 8EdRQxTKv3M76LyH0ocRX1ZQGylz5N2M4+t+GK9d7mo01S55pjnmn6fQUalIoUme35S3 79hg== X-Received: by 10.194.8.163 with SMTP id s3mr2666958wja.41.1372250137545; Wed, 26 Jun 2013 05:35:37 -0700 (PDT) MIME-Version: 1.0 Sender: maxpower57@gmail.com Received: by 10.194.124.52 with HTTP; Wed, 26 Jun 2013 05:35:16 -0700 (PDT) In-Reply-To: References: Date: Wed, 26 Jun 2013 07:35:16 -0500 X-Google-Sender-Auth: -BYiRmkgGX083DnGoQP7oZM1XJk Message-ID: To: Sherif Ramadan Cc: Florin Patan , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=047d7b5d253efdd27404e00de1da Subject: Re: [PHP-DEV] Request for comments - new PHP feature: return typing From: Max@MaxVandervelde.com (Maxwell) --047d7b5d253efdd27404e00de1da Content-Type: text/plain; charset=ISO-8859-1 Sherif, I have to disagree with you on this in the same way that we have type hints for input variables on methods, this could also be useful. Consider an interface with a getter method defined. As it stands now in php, they're not entirely useful, since you can't define what type of object your expecting from the implementer. That greatly reduces the functionality of an interface and this proposal gives solution to that problem. Florin, I think the overall concept is good, I want to see something like this in the future, but I think this might be the wrong approach. I don't think we should be hinting primitives. if we're going to have a return type hint on a method, I would have it match the functionality of the input type hints for functions we already have, where you can hint at an instance of an object type, such as Iterator or StdClass, or an array, but not hint primitives such as int and string, like I currently see in the draft. Also, you should be aware that your proposed syntax for the return type ( ) is interfering with: https://wiki.php.net/rfc/protocol_type_hintingand seems unnecessary to me. On Wed, Jun 26, 2013 at 5:08 AM, Sherif Ramadan wrote: > On Wed, Jun 26, 2013 at 5:50 AM, Florin Patan > wrote: > > > Hello PHP internals, > > > > > > Currently PHP doesn't support what could be a good feature for code > > quality, return typing (or if you prefer to call it: return type > > hinting). > > > > Since the procedure says that before any real RFC should be done, the > > interest for this topic should be gauged, I've drafted something here: > > https://gist.github.com/dlsniper/5863012 > > > > The goals for this discussion are: > > - gauge the interest in having such a feature in PHP.NEXT > > - find weak spots in the draft > > - find potential problems with the suggested way of doing the > > implementation > > - determine if the advantages would outweigh the disadvantages and > > this should go forward as a RFC > > > > As you can see, the draft doesn't include any patch and since my C > > skills are mostly nonexistent, I currently can't provide one > > unfortunately. I would however love to do it with proper guidance from > > someone that could spare some of his/her time to help me out. > > > > This said, thank you very much for your time, consideration and > > feedback on this subject. > > > > > > Best regards, > > Florin > > ---- > > Florin Patan > > https://github.com/dlsniper > > http://www.linkedin.com/in/florinpatan > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > PHP really doesn't need function return type declaration since PHP doesn't > even support variable declaration. This would make sense in a language like > C or C++ where the compiler can do static analyses to determine potential > errors before compiling. > > Since PHP is a loosely typed dynamic language, however, the fact that a > function can return something of any type is quite liberating and it turns > out that you don't need the static analyses to write better code with PHP, > because most of the errors static analyses helps you with aren't really the > things you are most concerned with. For example consider the following bug: > > function userTimezone($userId, PDO $db) { > $result = []; > if (!($stmt = $db->prepare('SELECT `timezone` FROM users WHERE > users.`id` = ?'))) { > return false; > } > if ($stmt->execute([$userId])) { > $result = $stmt->fetchAll(PDO::FETCH_ASSOC); > } > return array_pop($result); > } > ?> > > If in the above code PDO::prepare returns false (upon failure) for any > reason $stmt will not be of the expected type. In that case the function > returns a boolean false to indicate failure (or it can throw an exception > as well). If the Exception is throw the function's return value is still > implicitly null. However, the return type of the function isn't what we > actually care about here, because it doesn't solve our real problem. The > real problem we care about is the fact that the function failed and > couldn't keep its promise. If we force the function to only return an array > we can't tell the difference between an empty result set or a failure case > unless we strictly stick to exceptions. In none of those cases was the > function's return value type hint helpful. > --047d7b5d253efdd27404e00de1da--