Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53088 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1713 invoked from network); 6 Jun 2011 22:16:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Jun 2011 22:16:19 -0000 X-Host-Fingerprint: 208.107.178.23 host-23-178-107-208.midco.net Date: Mon, 06 Jun 2011 18:16:18 -0400 Received: from [208.107.178.23] ([208.107.178.23:25736] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/F3-23189-2B15DED4 for ; Mon, 06 Jun 2011 18:16:18 -0400 Message-ID: To: internals@lists.php.net References: <4DED3A29.1090209@sugarcrm.com> <4DED43D4.4070006@sugarcrm.com> User-Agent: slrn/pre1.0.0-18 (Linux) X-Posted-By: 208.107.178.23 Subject: Re: [PHP-DEV] Callable typehint From: weierophinney@php.net (Matthew Weier O'Phinney) On 2011-06-06, Stas Malyshev wrote: > > Like I mentioned in the other thread (which I probably should had > > repeated here), a lot of libs/frameworks are using the 'Closure' > > typehint for callbacks. > > Well, they are wrong (unless they mean to use only closures and not > callbacks). But that's no reason to do wrong thing in the language > too. > > > The problem with that is a function name as a string and > > array("classname", "functionname") are considered is_callable(). To > > get through the intentions of the Closure hint, I would have to wrap > > the actual callback in a closure - which doesn't make any sense. > > "callable" is not actually even a type. If we make it a language > construct, then why 'authenticated DB connection', 'name of readable > file', 'valid stream URL', 'validated XML', 'string in UTF-8 no longer > than 255 characters' or 'balanced binary tree' is not a language > construct? I don't think we need to put every data check into the > language, especially that it actually makes it worse - you can > gracefully handle user-space check, but not a strict typing error. The point, though, is that with such a typehint available, we can reduce boilerplate code like the following: public function addCallback($callback) { if (!is_callback($callback)) { throw new InvalidArgumentException(); } The typehint makes this simpler: public function addCallback(callback $callback) which allows us to rely on PHP's native error handling. I, for one, would love to see this. -- Matthew Weier O'Phinney Project Lead | matthew@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc