Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:25678 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91763 invoked by uid 1010); 14 Sep 2006 05:54:55 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 91748 invoked from network); 14 Sep 2006 05:54:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Sep 2006 05:54:55 -0000 Authentication-Results: pb1.pair.com header.from=tslettebo@broadpark.no; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=tslettebo@broadpark.no; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain broadpark.no from 80.202.4.58 cause and error) X-PHP-List-Original-Sender: tslettebo@broadpark.no X-Host-Fingerprint: 80.202.4.58 osl1smout1.broadpark.no Solaris 9 Received: from [80.202.4.58] ([80.202.4.58:58851] helo=osl1smout1.broadpark.no) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 71/73-45066-C8EE8054 for ; Thu, 14 Sep 2006 01:54:55 -0400 Received: from osl1sminn1.broadpark.no ([80.202.4.59]) by osl1smout1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with ESMTP id <0J5K008XZJ2HUQ40@osl1smout1.broadpark.no> for internals@lists.php.net; Thu, 14 Sep 2006 07:54:17 +0200 (CEST) Received: from pc ([80.203.129.59]) by osl1sminn1.broadpark.no (Sun Java System Messaging Server 6.1 HotFix 0.05 (built Oct 21 2004)) with SMTP id <0J5K00177J2HI0E0@osl1sminn1.broadpark.no> for internals@lists.php.net; Thu, 14 Sep 2006 07:54:17 +0200 (CEST) Date: Thu, 14 Sep 2006 07:54:34 +0200 To: Brian Moon Cc: internals@lists.php.net Message-ID: <006701c6d7c2$414424d0$9a02a8c0@pc> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 X-Mailer: Microsoft Outlook Express 6.00.2800.1807 Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 7BIT X-Priority: 3 X-MSMail-priority: Normal References: <0a1301c6d64f$8af01c70$a900000a@adstate.local> <74293252.20060912230042@marcus-boerger.de> <00b501c6d6b4$632b7e90$9a02a8c0@pc> <10845a340609130053n395632f4ka5bbed3e49ce6ad2@mail.gmail.com> <0d4001c6d71a$fa124260$a900000a@adstate.local> <45081ABF.2020804@dealnews.com> <005301c6d768$7a5fb0e0$9a02a8c0@pc> <45087C81.3030008@dealnews.com> Subject: Re: [PHP-DEV] Return type hints From: tslettebo@broadpark.no (=?UTF-8?Q?Terje_Sletteb=C3=B8?=) >From: "Brian Moon" > > --- Start --- > > > > class Something > > { > > public function __construct() > > { > > // Oops, forgot to initialise $this->something... > > } > > > > public function f() > > { > > return $this->something; > > } > > > > private $something; > > } > > > > > > error_reporting(E_ALL); > > > > $something=new Something; > > > > echo $something->f()+10; // Prints "10". > > If you can't trust the return values of your methods, I would use: > > $var = $something->f(); > > if($var!==NULL){ > echo $var+10; // Prints "10". > } Right... And you can always use return codes, instead of exceptions or trigger_error(). The problem is that there's nothing that _enforces_ this checking, and since it adds verbosity, it's typically not done. If you don't believe me have a look at some of all of the C code out there, which typically to a large degree don't check return values (and it shows... Programs segfaulting and the like, if something unexpected happens), as it makes the program convoluted and messy, and is easily forgotten. It's for reasons like this that exceptions (and possibly trigger_error()) were invented in the first place. It's similar with the example above: Type-checked return values means you don't have to write all these tedious manual checks, just to ensure program correctness (and had the return type hint been mandatory, there's no way you could forget it or not bother with it, either).. > However, its crap like this that reminds me why I don't use PHP OOP for > all my code. I can find no non-OOP code that behaves this way. The above was a contrived example, meant to illustrate the point. What's so bad about it? That it doesn't check the return value? Well... having to check the return value, to ensure things didn't go wrong is something I left a looong time ago, when I was programming C, before I started with C++ and Java, which gives better ways of handling this, and have only had to take it up again, in PHP. > Even my > favorite PHP trick, using settype() to initialize vars, does not set the > var to NULL. settype() is still a manual operation - there's no way to automatically guarantee initialisation. In a language like C++, the class members are default-initialised, unless you explicitly initialise them, so there's no way you can forget it or ignore it. The result: More robust programs. Regards, Terje