Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80650 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27889 invoked from network); 16 Jan 2015 16:53:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jan 2015 16:53:44 -0000 Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.181 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.181 mail-vc0-f181.google.com Received: from [209.85.220.181] ([209.85.220.181:53643] helo=mail-vc0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/C0-23044-51249B45 for ; Fri, 16 Jan 2015 11:53:43 -0500 Received: by mail-vc0-f181.google.com with SMTP id le20so7009691vcb.12 for ; Fri, 16 Jan 2015 08:53:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=C+7mj8SSG/AQsK3CZm+B25DXYOIjm+PYBmlGyEwe59Y=; b=LFhWP2bOo0ux7lDyrjZhty6Kgl7G7oG2/RpJVvE/9RbDZEa1JCoj5Q/e+zIcHuXSyJ nmlLHA75RrPPUdjgkgvSJBLEm3pz54tuf562HtGZPVqHQQ7CvYgLKLjqMWajAeAMJhaX nL2Rmzd0fcblFUg5zy7Z0Ek1syyc5hL0UQ9wiTvtAX1ZEW9jhfqbH3viDOEnkNnQsCN9 t5/nXlw7EQcVrJlgLCEF/RmkY9HAmqLNObTJrI0yFrq7RC7MskNKLmWln4A/vHEpGW2q JTF6KCgVB9mCRyb3mUrzTDZcJNt6ieUshA36dJJvimf6Okzs7TXMZ+h9mjPMwDnARc1r Y4xQ== X-Gm-Message-State: ALoCoQn5uyOc4yKndRnsXYQ0fc/Zd47JxSBbzziaCR0znYxl9//wwQpFaUWXN5hugIMcPAQ0ozYU1sXQAkbmB53q89U6OGryKQzLfxqmgT4irZeSC0+gfnPTkCq3OS+QDPUVA6NC+l0eDuKBWWghWUCPvC6Hqd7Geg== MIME-Version: 1.0 X-Received: by 10.220.91.78 with SMTP id l14mr8073384vcm.4.1421427218261; Fri, 16 Jan 2015 08:53:38 -0800 (PST) Received: by 10.52.26.40 with HTTP; Fri, 16 Jan 2015 08:53:38 -0800 (PST) In-Reply-To: <54B93542.8060105@gmail.com> References: <8DCD1B72-C81D-499E-B455-E4A042CD76E6@ajf.me> <4E2073DE-0951-498C-97BB-DDAC094F11FA@ajf.me> <9a033dd1f223f854e760924d118ab812@mail.gmail.com> <2ae0164cb9b9bf1c974d7a3c60af0466@mail.gmail.com> <6105ea99002e634373c09685310e26a6@mail.gmail.com> <54B90431.10903@gmail.com> <54B925B8.1010601@gmail.com> <54B93542.8060105@gmail.com> Date: Fri, 16 Jan 2015 20:53:38 +0400 Message-ID: To: Rowan Collins Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=047d7b343e5c6b438a050cc7d044 Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2 From: dmitry@zend.com (Dmitry Stogov) --047d7b343e5c6b438a050cc7d044 Content-Type: text/plain; charset=UTF-8 I was just surprised by the current behavior :) $ cat ops.php $ sapi/cli/php -n ops.php Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given in ops.php on line 2 It too me time to realize what is going on :) Thanks. Dmitry. On Fri, Jan 16, 2015 at 6:58 PM, Rowan Collins wrote: > Andrey Andreev wrote on 16/01/2015 15:37: > >> With no attempt to argue, I just thought of another example, inspired >> by the ones given in the RFC - it is fairly common for functions >> dealing with times to only accept a UNIX timestamp or otherwise just a >> single kind of a time unit, such as only a count of minutes or days. >> >> So where '7 years' is passed and and accepted when using a weak hint >> (by truncating the trailing non-numeric characters), a strict hint on >> the other hand may prevent the input of incorrect data (given that >> it's not years that are expected, of course). >> > > That's a nice example of the advantage, actually, thank you. > > However, it makes me think of another approach to the whole situation, > which is to make more use of Value Objects (now that the implementation of > objects is no longer a huge performance hog). Obviously, for a timestamp, > there is the built-in DateTimeInterface, but even if there weren't, it > would be trivial to create something which existed only for type-checking a > kind of sub-classed integer: > > class UnixTimestamp { private $value; public function get_value() { return > $this->value } public function __construct($new_value) { /* validate input > */ } } > > Obviously, the validation would still need to be written, but it would be > tucked away in the shared class, rather than pasted as boilerplate at the > top of every function. > > In many situations, the type of the argument isn't really "int" or > "string", it's "mode" (from an enum or bitmask), or "text label" (maybe > needs translation support) etc. So a library that really wants to document > its type system should be providing wrappers for all these value types, at > which point it can type hint them all by class or interface anyway. > > Scalar type hinting falls into the in-between category of "I want my code > to be type-safe, but using a very flat type system which makes only loose > guarantees about the data". > > Regards, > -- > Rowan Collins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --047d7b343e5c6b438a050cc7d044--