Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33273 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24239 invoked by uid 1010); 19 Nov 2007 00:24:09 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 24224 invoked from network); 19 Nov 2007 00:24:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Nov 2007 00:24:09 -0000 Authentication-Results: pb1.pair.com smtp.mail=hannes.magnusson@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=hannes.magnusson@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 64.233.162.237 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: hannes.magnusson@gmail.com X-Host-Fingerprint: 64.233.162.237 nz-out-0506.google.com Received: from [64.233.162.237] ([64.233.162.237:32591] helo=nz-out-0506.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 54/34-31009-5A7D0474 for ; Sun, 18 Nov 2007 19:24:09 -0500 Received: by nz-out-0506.google.com with SMTP id x7so1097220nzc for ; Sun, 18 Nov 2007 16:24:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=A1EXjTPdWH139cZNwOyvUbRr0G1IkQbiX06NUxOaxyg=; b=SWTYwp7DL+aY8Ucsq/IkGJwzFVAvIUwsXxWa4xOT4/sBPoVhNosoAAX656I6jsMmXxoNxL9adTMCyqvyxJeL0K3MB1nwCh7a15B60iJpoTUk41vcTL3Eub76TaV55t66Urz245Ca5G0GCraR7aUQMATDo2Ivav9VbEf/LuOre40= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=DJDLyWD/PU8Lg+oAICM0WmJugwBn0C1HFS5LCL/k8Vmtbx4jBCZHByLBggrvcmif0mGXeeVlk7xtwDbncFgmRZboEo7ByNNhl55D3rtNGeIUtvVWzwbA7GHZCPflkmVWhPbOVodGpUlfDkcx635aoJCUmMfRHgV/xTmE6Io1CnQ= Received: by 10.142.147.15 with SMTP id u15mr946810wfd.1195431841949; Sun, 18 Nov 2007 16:24:01 -0800 (PST) Received: by 10.142.192.6 with HTTP; Sun, 18 Nov 2007 16:24:01 -0800 (PST) Message-ID: <7f3ed2c30711181624r7a0c77ddh6f9e86fa3c862133@mail.gmail.com> Date: Mon, 19 Nov 2007 01:24:01 +0100 To: "David Coallier" Cc: "Cristian Rodriguez" , internals@lists.php.net In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1195140437.23612.5.camel@sbarrow-desktop> <10610581037.20071115172619@marcus-boerger.de> <7d5a202f0711181452h3b6a26b8x5b838b585c552765@mail.gmail.com> Subject: Re: [PHP-DEV] [PATCH] Optional scalar type hinting From: hannes.magnusson@gmail.com ("Hannes Magnusson") 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: 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)); -Hannes