Bug #71870 [Com]: [][index] syntax emits a "Undefined variable: offset" notice

From: Date: Mon, 21 Mar 2016 16:03:16 +0000
Subject: Bug #71870 [Com]: [][index] syntax emits a "Undefined variable: offset" notice
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200000@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71870&edit=1 ID: 71870 Comment by: nish dot aravamudan at canonical dot com Reported by: nish dot aravamudan at canonical dot com Summary: [][index] syntax emits a "Undefined variable: offset" notice Status: Closed Type: Bug Package: Arrays related Operating System: Ubuntu 16.04 PHP Version: 7.0.4 Assigned To: nikic Block user comment: N Private report: N New Comment: Great, thank you! Previous Comments: ------------------------------------------------------------------------ [2016-03-21 15:48:01] nikic@php.net Good timing! I've fixed this bug yesterday: https://github.com/php/php-src/commit/1f6d27d3d2cf8a5113946a55a297441bb4c70ddf It still does throw the "indirect modification" notice, because offsetGet doesn't return by reference. ------------------------------------------------------------------------ [2016-03-21 15:15:28] nish dot aravamudan at canonical dot com Description: ------------ https://3v4l.org/T9HLm Note that with PHP5, no "undefined variable offset" is emitted. I believe this is a BC break in PHP7.0. Also, I found this page: https://wiki.php.net/rfc/uniform_variable_syntax, which explicitly mentions: "E.g. empty([]['a']) will no longer throw an undefined offset notice." This is seen with php-zeta-console-tools tests, and leads to a test failure. Test script: --------------- <?php error_reporting(-1); class obj implements ArrayAccess { private $container = array(); public function __construct() { $this->container = array( "one" => 1, "two" => 2, "three" => 3, ); } public function offsetSet($offset, $value) { if (is_null($offset)) { $this->container[] = $value; } else { $this->container[$offset] = $value; } } public function offsetExists($offset) { return isset($this->container[$offset]); } public function offsetUnset($offset) { unset($this->container[$offset]); } public function offsetGet($offset) { return isset($this->container[$offset]) ? $this->container[$offset] : null; } } $obj = new obj; $obj[][0] = 'Append'; Expected result: ---------------- No notice to be emitted. Actual result: -------------- Notice: Undefined variable: offset in /in/T9HLm on line 32 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71870&edit=1

« previous php.bugs (#200000) next »