php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81338 readonly properties cannot be used with iterators
Submitted: 2021-08-06 13:42 UTC Modified: 2021-08-06 13:53 UTC
From: julien dot boudry at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 8.1.0beta2 OS: All
Private report: No CVE-ID: None
 [2021-08-06 13:42 UTC] julien dot boudry at gmail dot com
Description:
------------
https://3v4l.org/edgYU/rfc#vgit.master

Internal array pointer can not be modified for readonly properties.
This strongly limits the possibility of implementing Iterator interfaces using them. The following example is simpler, but still follows the same principle.

These properties would be much more useful if the pointer remained free, as the data itself is not impacted.

Test script:
---------------
<?php

class A {

    public function __construct (public readonly array $ary = [0,1,2,3])  {}
}

$obj = new A;

var_dump(current($obj->ary));

next($obj->ary);

var_dump(current($obj->ary));


Expected result:
----------------
int(0)
int(1)

Actual result:
--------------
int(0)

Fatal error: Uncaught Error: Cannot modify readonly property A::$ary in /in/CBCan:12
Stack trace:
#0 {main}
  thrown in /in/CBCan on line 12

Process exited with code 255.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-08-06 13:53 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-08-06 13:53 UTC] nikic@php.net
Modifying IAP is still a modification, so declining this feature request.

Generally, I would recommend to never use the IAP and instead use either ArrayIterator or generators.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 19:01:29 2024 UTC