Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:9623 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58374 invoked by uid 1010); 1 May 2004 20:09:32 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 58330 invoked from network); 1 May 2004 20:09:32 -0000 Received: from unknown (HELO shiva.mind.de) (212.42.230.204) by pb1.pair.com with SMTP; 1 May 2004 20:09:32 -0000 Received: from BAUMBART (p508EB34D.dip.t-dialin.net [80.142.179.77]) by shiva.mind.de (Postfix) with ESMTP id C46B197B58 for ; Sat, 1 May 2004 22:09:30 +0200 (CEST) Date: Sat, 1 May 2004 22:09:32 +0200 Reply-To: Marcus Boerger X-Priority: 3 (Normal) Message-ID: <243202120.20040501220932@marcus-boerger.de> To: internals@lists.php.net MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: [RFC] Type hints From: helly@php.net (Marcus Boerger) Hello internals, while working with php 5 the last days i find it more and more annoying that we allow NULL with type hints. From my perspective allowing NULL with typehints would happen only very rare. So i looked into the issue once again and prepared a patch. first i looked back the mails for this issue and found three possible solutions: 1) (Zeev) allow NULL only when it is the first default argument. Here the disadvantage is that you cannot have non default parameters after one that allows NULL. Also this would introduce a BC break if we'd go this way after 5.0 is out because it changes the standard behavior. 2) (Andi) introduce 'not null' after php 5.0. This won't introduce a BC break. But it would require two new keywords 'NOT' and 'NULL'. The latter is not a problem because it is a define at the moment and the patch (a) shows that it easy to change it into a keyword. The problem is the new keyword 'not' which would introduce a BC break for everyone using the word 'not' already. 3) (Myself) introduce 'or null'. Obviously this would introduce a BC break when implemented after PHP 5.0 is out. The advantage is that it only requires NULL as an additional keyword (see above) and that it reflects the code it replaces. The replacement code is to check whether the parameter is an instance of the typehint "or" if specified check if it is null: func(Classname or NULL $param) {} replaces func($param) { if ($param instanceof Classname or is_null($param)) { // ok } else { // error } } Having listed the three ideas i think we must address this issue now and cannot delay it until after php 5.0 is out. Because of 2's additional keyword and 1's disadvantage i like 3 most. The patch (b) implements it and adds a testfile. Patch (c) also implements typehints for 'array' and 'class'. Array checks the parameter for being an array and class checks the parameter to be an instance of any class. These two we agreed to do already and so i added an updated patch for it which also includes the 'or null'. (a) http://somabo.de/php/ext/ze2/ze2-null-patch-20040501.diff.txt (b) http://somabo.de/php/ext/ze2/ze2-type-hints-null-20040501.diff.txt (c) http://somabo.de/php/ext/ze2/ze2-type-hints-20040501.diff.txt p.s.: As a sideeffect these patches speedup NULL handling. Best regards, Marcus