Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91205 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 74310 invoked from network); 11 Feb 2016 13:14:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Feb 2016 13:14:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.169 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.161.169 mail-yw0-f169.google.com Received: from [209.85.161.169] ([209.85.161.169:33363] helo=mail-yw0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A4/7A-25203-B498CB65 for ; Thu, 11 Feb 2016 08:14:52 -0500 Received: by mail-yw0-f169.google.com with SMTP id u200so38418363ywf.0 for ; Thu, 11 Feb 2016 05:14:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=YYREA37NdIm5BJR+LNx2h2/Iy8TGMFinBFhzA448Qa0=; b=QqduIrtYDIS8O8ckHq20iQFoLlk5RadeEOypibAl8H4jpAhMlahmounfZyvS8JPAET SfrR6P8XwEYfjIwi0Qw3PhVV6x1rx2iRNmAEVD8eR6AgwJl+/po1nBfJZBfGpi4r7J25 iHEIcbx5wXBr1eNvPJ1QVWNL9/TnZyxO/oZ5dspyL3/cutGBMKT1gbUshmIvwbLF3cUd 7zlgFnRGa4kHOn4X9SOrabnIjii1mcAgcUtoEIoWqwJItZpyiyb6GCys+Nmbrww6fOqO 0gDLGntKhxF511JK6UBy0N2c8VVVCkCUee7nuH7GSW9jjmmkTF7Ht/eQxuk4NhTnCsNP zuoA== 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=YYREA37NdIm5BJR+LNx2h2/Iy8TGMFinBFhzA448Qa0=; b=mungBuztG+FfoS67bROZDCaWOyvcMtU9dhf0VjRkOGfgNzqDG5wRYO0UQNIzsqv6hR qJVG4EgEdo6OKo+714C9YTSeE4zTPgw1fyUEeWYCfzD4G1TnJvXkPPwfby/9+UoZYrYn en+wd98fq1HZ4xkFB72z6eI4ABDxD5D5xSXU9mjqV27u4UvU57ZNlEw6hpE2CuKyOTrw rh0I6d/Tv46tUzhui3/qLWOBkf2dsL88qVfCnpp/8pJeZURq4KPXlSCIQ8jvbi+h4lyQ PPW0aFCnI3RpkH47XsWpfnDCb94URpEge0fJQevqAFTUUaubFeQuVRAFyt18Y5+iyi5X 7IZg== X-Gm-Message-State: AG10YOQsL8AqzXIBhulIkM9yHQA83lxoKklYxhoThYuAz91pnrB8eWsLQVK/aLff27Cm66CX9UuYsdLgL1r60g== MIME-Version: 1.0 X-Received: by 10.129.50.15 with SMTP id y15mr12720323ywy.305.1455196488730; Thu, 11 Feb 2016 05:14:48 -0800 (PST) Received: by 10.129.148.70 with HTTP; Thu, 11 Feb 2016 05:14:48 -0800 (PST) In-Reply-To: <56BC829C.1040608@gmail.com> References: <56A3A01F.1020500@php.net> <56BB4A5F.3060906@php.net> <56BC29C8.9070308@gmail.com> <56BC3372.1010308@gmail.com> <56BC7BB6.6070705@gmx.de> <56BC829C.1040608@gmail.com> Date: Thu, 11 Feb 2016 14:14:48 +0100 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11408daeca1135052b7e55a8 Subject: Re: [PHP-DEV] [RFC] Generalize support of negative string offsets From: nikita.ppv@gmail.com (Nikita Popov) --001a11408daeca1135052b7e55a8 Content-Type: text/plain; charset=UTF-8 On Thu, Feb 11, 2016 at 1:46 PM, Rowan Collins wrote: > Christoph Becker wrote on 11/02/2016 12:16: > >> Appending to an array always adds a single element only, consider >> >> $a = [1,2,3]; >> $a[] = [4,5]; >> >> The suggested syntax for strings would concatenate an arbitrary amount >> of elements (characters) to a string. IMHO, this would not be >> consistent, but rather confusing. The alternative interpretation to >> append the first character only, would be confusing as well (besides >> there would be issues with regard to multibyte character encodings). >> > > Indeed, this is a big problem with making the string offset functionality > richer in general, because PHP has no "char" type, only a single-character > string. This leads to some odd things: > > // string[0] returns a one-char string, which has an element [0], so you > can keep adding [0] for as long as you like: > $foo = 'abc'; > echo $foo[0][0][0][0][0]; // a > > // assignment to an offset only overwrites one character, but the source > can be a string of any length: > $foo = 'abc'; > $foo[0] = 'zzz'; > echo $foo; // zbc > > String offsets simply can't behave like array offsets within PHP's current > type system, which is why I favour dedicating {} syntax to them The issue here is not an inherent problem with string offsets, it's simply yet another artifact of PHP's overly lenient nature. If I create an IntArray class that implements ArrayAccess, I don't think you would be much surprised if doing $intArray[$i] = "123abc" would end up storing the value int(123), as this just conforms to PHP's usual silent, lossy type conversion semantics. It's the same with string offsets, just with the conversion happening from string to single-character-string. Just having a different syntax, writing $foo{0} = 'zzz' instead of $foo[0] = 'zzz' does not make the implicit truncation behavior any more obvious or reasonable. If we want to actually make it less confusing, what we should do is throw a warning if such a truncation occurs. And that's something that can be done independently of syntax. Similarly, Stas' comments on how things like $string[0] ^= "\xf0" do no work, are not a reason to promote a different string offset syntax -- that wouldn't actually *solve* anything. Instead we should strive to make these things work as expected. If I can write $string[0] = $string[0] ^ "\xf0" it stands to reason that $string[0] ^= "\xf0" should work as well. Nikita --001a11408daeca1135052b7e55a8--