Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71325 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78450 invoked from network); 20 Jan 2014 15:34:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jan 2014 15:34:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@tutteli.ch; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@tutteli.ch; sender-id=pass Received-SPF: pass (pb1.pair.com: domain tutteli.ch designates 80.74.154.78 as permitted sender) X-PHP-List-Original-Sender: php@tutteli.ch X-Host-Fingerprint: 80.74.154.78 ns73.kreativmedia.ch Linux 2.6 Received: from [80.74.154.78] ([80.74.154.78:51449] helo=hyperion.kreativmedia.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 50/AE-02192-5124DD25 for ; Mon, 20 Jan 2014 10:34:46 -0500 Received: (qmail 15933 invoked from network); 20 Jan 2014 16:34:43 +0100 Received: from heim-032-99.raab-heim.uni-linz.ac.at (HELO RoLaptop) (193.171.32.99) by ns73.kreativmedia.ch with (AES128-SHA encrypted) SMTP; 20 Jan 2014 16:34:43 +0100 To: "'Joe Watkins'" , References: <002d01cf1234$01e6dc60$05b49520$@tutteli.ch> <004c01cf123d$35730870$a0591950$@tutteli.ch> <52D71748.1090402@googlemail.com> <52D71FAE.8030002@ajf.me> <005001cf124f$3a40df00$aec29d00$@tutteli.ch> <20140116110127.202079vzjsj76n7b@webmail.tutteli.ch> <0B.B1.24763.139B7D25@pb1.pair.com> <002001cf12da$2bfbda90$83f38fb0$@tutteli.ch> <52DCA3E7.80602@lerdorf.com> <52DCED71.3020207@pth reads.org> In-Reply-To: <52DCED71.3020207@pthreads.org> Date: Mon, 20 Jan 2014 16:34:40 +0100 Message-ID: <006301cf15f5$22f8df60$68ea9e20$@tutteli.ch> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQGahXeLh/KZbd8XAeJ9KP0Xitay+ALEucEqAx8utqACqtBDzQKRtClgAWWf+lcCX4Kx1gHC+GyIAgNcg4YBEz9CWQDvBFdyAu6br9kCL2LDqQKSKc2VAbc66pQDJngE8QEOUWWkAR3dyz8Bml53TpnOaZsA Content-Language: de-ch Subject: RE: [PHP-DEV] Introducing "Array Of" RFC From: php@tutteli.ch ("Robert Stoll") Hey Joe, > I don't want to set out to do anything that is bad, however, I think > the performance thing is not really a massive concern, after all it's > not very difficult for something innocent to incur a measurable overhead: > > https://eval.in/91920 It can be fairly fast for one call only, but imagine you have those type hints all over the place. I really don't think it is a good idea to introduce something which we both now is far from ideal. Yet, I like the idea behind this RFC and think we should think it over and come up with a better implementation. Your last comment was actually thought-provoking > The idea to change the hashtable wouldn't work anyway (for the OP of > the idea): you can have a reference to a variable that is a member of an > array and change it's type without executing any opcodes related to > hashtable manipulation. So I sat down and coded an example in PHP. Maybe it can help this RFC to go further. You can find it here: https://github.com/robstoll/phputils/blob/master/TypedArray.php Seems like the implementation is robust enough for the dirty reference hack (as mentioned by you) by chance. Yet, one could still inject invalid types via Reflection. It's not possible to prohibit that as far as I know. Brief overview of the implementation - works for all types (bool, int, float, string, array, resource, callable, and class/interface) - initialisation in two different ways $foos = new TypedArray(Types::T_CLASS_OR_INTERFACE, "Foo"); $bars = TypedArray::createFromArray(Types::T_INT, [1,2,3, 'a'=5, 'a'=>9]); - type hinting can be used as follows: /** * @param TypedArray $arr */ function foo(TypedArray $arr){ $arr->checkIsSameType(Types::T_CLASS_OR_INTERFACE, "Foo"); } I have not yet implemented Iterator as interface but that could be added easily This implementation has of course drawbacks compared to the RFC: - it is not obvious what type TypedArray contains just from reading the signature (needs the additional documentation) - needs one extra line of code to check if actually the given TypedArray is of the correct type And advantages: + is already possible with the current PHP version + type hint check O(1) + add element check O(n) + one can still pass a normal array with the initialise syntax (does more or less the same as your implementation): foo(TypedArray::createFromArray(Types::T_INT, $arr); If we would substitute the one additional line with some syntactic sugar then we would have already a nice implementation IMO. Cheers, Robert