Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:33277 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61279 invoked by uid 1010); 19 Nov 2007 02:50:19 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 61262 invoked from network); 19 Nov 2007 02:50:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Nov 2007 02:50:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.coallier@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=david.coallier@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 64.233.162.234 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: david.coallier@gmail.com X-Host-Fingerprint: 64.233.162.234 nz-out-0506.google.com Received: from [64.233.162.234] ([64.233.162.234:37555] helo=nz-out-0506.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3C/D9-31009-9E9F0474 for ; Sun, 18 Nov 2007 21:50:18 -0500 Received: by nz-out-0506.google.com with SMTP id x7so1124308nzc for ; Sun, 18 Nov 2007 18:50:15 -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:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; bh=BnpQrAZ1l6t0AzgfTkpmlGzrOcGiufqqE6jAqqbdge0=; b=Zyhqui/xiNktjMI5j5x7FK5CE5uR4RrZKSK2bl5r5gN2+SM8uuSd8tmZdGs+/I0BMyNRE1b/mEZF78JkeA41fH4DIgCCNzev3hKurB13YAkosu7C7UxnyEjDQR8VwKWeklhe3CO0DivwAmWYqCvIITtnBzMdlGnMGMg5vXrb6zg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=eNju8I1Zkv+l4sc5L5RX1JtAuKHU6GYzAIDrMpkpZIap4rHlkapz2FunvoNlUMGW4f7h8CW4H0Mj61my8c8hd/W1UowtQ8Ki2b3rW7XZpqcf7qMaAPJ0acj4yC3/yZ4+t/TGc50bb3GZ7AdexM252vRm72k7LX8eBh0KOTClOGM= Received: by 10.142.229.4 with SMTP id b4mr952366wfh.1195440614958; Sun, 18 Nov 2007 18:50:14 -0800 (PST) Received: by 10.143.41.16 with HTTP; Sun, 18 Nov 2007 18:50:14 -0800 (PST) Message-ID: Date: Sun, 18 Nov 2007 21:50:14 -0500 Sender: david.coallier@gmail.com To: "Hannes Magnusson" Cc: "Cristian Rodriguez" , internals@lists.php.net In-Reply-To: <7f3ed2c30711181624r7a0c77ddh6f9e86fa3c862133@mail.gmail.com> 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> X-Google-Sender-Auth: 5c0cce09bdf8ddd1 Subject: Re: [PHP-DEV] [PATCH] Optional scalar type hinting From: davidc@php.net ("David Coallier") 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