Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33301 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5795 invoked by uid 1010); 19 Nov 2007 11:37:27 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 5778 invoked from network); 19 Nov 2007 11:37:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Nov 2007 11:37:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@googlemail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.146.180 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@googlemail.com X-Host-Fingerprint: 209.85.146.180 wa-out-1112.google.com Received: from [209.85.146.180] ([209.85.146.180:58881] helo=wa-out-1112.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4D/D2-22759-D6571474 for ; Mon, 19 Nov 2007 06:37:24 -0500 Received: by wa-out-1112.google.com with SMTP id l24so2136597waf for ; Mon, 19 Nov 2007 03:37:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=uHrMjiN2izthLdkjVJV75TNT96310xm5O1PrWAWiSr0=; b=ghBuXFwQ4k6cmISUQHQJqSyt65Tvr40rGBbfxJwUW6hfrK7eIGrWimM4XqLLNcd6ysdRFCB5I/7JLZX/Dog9+Srm3wa/Huc4EnL2tctUSL5A+gbVQbCbADD6iVAet5Zu2uPO55l+jkDgkvxQpo3rETTA2KZctO/ka6ObUl5F1Ms= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=sRQOxldCIyvU+JJwf0Q9Nf9W/ycT3q+UIDiFmWQOJ6ZY02891dSrPuMOwiLNEzzAUi2f2YsNkDVKbjWXWV/aHesNbRCRM1M7f8bi5znlu9cFpOnoPeyFQkhnq0YGvGwpCRYBIRI1PsG1WLQhAWh9A8YQBthe3kdCzEsEz3OAlyo= Received: by 10.114.144.1 with SMTP id r1mr432408wad.1195472225984; Mon, 19 Nov 2007 03:37:05 -0800 (PST) Received: by 10.114.210.8 with HTTP; Mon, 19 Nov 2007 03:37:05 -0800 (PST) Message-ID: <10845a340711190337q29224625l5dc24ed0cf4f815e@mail.gmail.com> Date: Mon, 19 Nov 2007 11:37:05 +0000 Reply-To: RQuadling@GoogleMail.com To: "Sam Barrow" Cc: "David Coallier" , "Hannes Magnusson" , "Cristian Rodriguez" , internals@lists.php.net In-Reply-To: <1195448765.12342.12.camel@sams-room> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1195140437.23612.5.camel@sbarrow-desktop> <10610581037.20071115172619@marcus-boerger.de> <7d5a202f0711181452h3b6a26b8x5b838b585c552765@mail.gmail.com> <7f3ed2c30711181624r7a0c77ddh6f9e86fa3c862133@mail.gmail.com> <1195448765.12342.12.camel@sams-room> Subject: Re: [PHP-DEV] [PATCH] Optional scalar type hinting From: rquadling@googlemail.com ("Richard Quadling") On 19/11/2007, Sam Barrow wrote: > I both like and dislike the fact that PHP is so loosely typed. It makes > it very easy, which I like, however it is sometimes not strict enough, > allowing for undetected errors, which of course I don't like. > > I think the ideal solution here is to make it a hybrid type of language, > where typing is dynamic, but still controllable using type hinting. You > can use whichever you want where you want. Functions can have 3 > parameters, two hinted and one not. > > Keep in mind this is not strict typing. Key word: "hinting". > > If all is 100% optional, your application can take advantage of the > flexibility of dynamic typing in places where it's needed, and also take > advantage of the control that type hinting gives you where it's > appropriate. Most languages either allow you to do whatever you want, > making some code chaotic and some bugs harder to detect, whereas some > are the complete opposite, imposing very strict limits on your > variables, and severely limiting your flexibility. PHP could be the > middle ground, with maximum flexibility, but also maximum control. > > I'm currently using my type hinting patch with PHP 5.3 for an > application I'm working on with about 10,000 lines of code and hundreds > of functions, big and small. I'm using type hinting in most of the > functions, but not all. It's been very helpful so far, especially with > the "scalar" and "number" type hints. > > On Sun, 2007-11-18 at 21:50 -0500, David Coallier wrote: > > On Nov 18, 2007 7:24 PM, Hannes Magnusson wrote: > > > On Nov 19, 2007 12:13 AM, David Coallier wrote: > > > > I was thinking at something along the lines of objects also for instance: > > > > > > > > $i = new Integer(33); > > > > > > > > function foo(Integer $var) { > > > > } > > > > > > > > foo ($i); else it emits a fatal error. But also if you do > > > > > > > > $i = "Name"; that would emit a fatal error because the value is > > > > suposed to be an int. This might look a bit too much like java, but as > > > > an extension it could be something quite interesting I believe. > > > > > > > > String, Object, Integer, Scalar, Float and what else. > > > > > > > > So thinking of something like > > > > > > > > $string = new String("Foo"); > > > > $string = "bar" or $string->setValue("Bar"); would do > > > > > > > > $float = new Float(4.242); > > > > $float->setValue('foobar'); // That emits an error > > > > $float->setValue(3.14159); > > > > > > > > echo $float; (__toString) or echo $float->getValue; to echo it's content/value > > > > > > > > and so on. > > > > > > That has got to be the worst idea I've heard on internals for over a month. > > > Besides, you can do this in userland already anyway: > > > > > > > haha :) Yes, you can do many things in userland. > > > > > > > class InvalidTypeException extends Exception {} > > > class UnknownTypeException extends Exception {} > > > > > > class Types { > > > const INTEGER = 1; > > > const FLOAT = 2; > > > const STRING = 3; > > > const OBJECT = 4; > > > const BOOLEAN = 5; > > > > > > private $val, $type; > > > > > > public function __construct($val, $type) { > > > $this->type = $type; > > > $this->setValue($val); > > > } > > > public function setValue($val) { > > > switch($this->type) { > > > case self::INTEGER: > > > if (!is_int($val)) { > > > throw new InvalidTypeException; > > > } > > > break; > > > > > > case self::FLOAT: > > > if (!is_float($val)) { > > > throw new InvalidTypeException; > > > } > > > break; > > > > > > case self::STRING: > > > if (!is_string($val)) { > > > throw new InvalidTypeException; > > > } > > > break; > > > > > > case self::OBJECT: > > > if (!is_object($val)) { > > > throw new InvalidTypeException; > > > } > > > break; > > > > > > case self::BOOLEAN: > > > if (!is_bool($val)) { > > > throw new InvalidTypeException; > > > } > > > break; > > > > > > default: > > > throw new UnknownTypeException; > > > > > > } > > > $this->val = $val; > > > } > > > public function getValue() { > > > return $this->val; > > > } > > > public function __toString() { > > > return (string)$this->getValue(); > > > } > > > } > > > > > > class Integer extends Types { > > > public function __construct($val) { > > > parent::__construct($val, Types::INTEGER); > > > } > > > } > > > class String extends Types { > > > public function __construct($val) { > > > parent::__construct($val, Types::STRING); > > > } > > > } > > > class Float extends Types { > > > public function __construct($val) { > > > parent::__construct($val, Types::FLOAT); > > > } > > > } > > > class Object extends Types { > > > public function __construct($val) { > > > parent::__construct($val, Types::OBJECT); > > > } > > > } > > > class Boolean extends Types { > > > public function __construct($val) { > > > parent::__construct($val, Types::BOOLEAN); > > > } > > > } > > > function type_hint_integer(Integer $val) { > > > echo $val, "\n"; > > > } > > > function type_hint_string(String $val) { > > > echo $val, "\n"; > > > } > > > function type_hint_float(Float $val) { > > > echo $val, "\n"; > > > } > > > > > > > > > > > > type_hint_integer(new Integer(123)); > > > type_hint_string(new String("string")); > > > type_hint_float(new Float(0.25)); > > > > > > > Nice implementation, so ? Still more code to include in your code. > > Anyways, the "Optional" part in the subject means ... optional. Which > > means that it does not *have* to be used and that the default hinting > > will still be loose. > > > > That gives the chance to anyone to use either strongly-typed code or > > loosely-typed code. You know as much as I do that it's quite easy to > > do (implement - like you just did), but if it's not in by default, > > people won't care about doing it or implementing it. > > > > On the other hand, if it's there, some people might be tempted to use > > it, and will. But hey.. php's a loosely typed language and it'll > > definitely stay that way. But only giving the choice to someone to use > > what he feels like is quite interesting in my opinion. > > > > Anyways, no need to get all grumpy, you can just say you don't like > > the idea, that'll do just as well. Anyways, that was a thought, it's > > quite easy to implement as an extension and would be light and simple > > for anyone to either use or ignore it. > > > > > > > -Hannes > > > > > > > > > > > -- > > David Coallier, > > Founder & Software Architect, > > Agora Production (http://agoraproduction.com) > > 51.42.06.70.18 What if type hinting just generated an E_NOTICE. Nothing more for the time being. Call it an experimental option. I already use hungarian notation for all my params ( cause I'm unimaginative with my var names I suppose!), so the type hinting would help me enforce this and reduce errors in the long run (code goes from me to other developers who only glance at the dox) -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"