Skip to content

Writing an offset into an uninitialized typed array property is reported as UninitializedProperty #14272

Closed

Description

@orklah
<?php
final class Bag
{
    /** @var array<string, string> */
    private array $values;

    public function __construct(string $name)
    {
        $this->values['name'] = $name;
    }

    /** @return array<string, string> */
    public function all(): array
    {
        return $this->values;
    }
}

https://psalm.dev/r/00cf304553

Actual: UninitializedProperty: Cannot use uninitialized property $this->values (line 9), and PropertyNotSetInConstructor (line 5).

Expected: No issue: PHP initialises an uninitialized array property on an offset write, so $this->values is ['name' => ...] after the constructor.

Activity

  1. psalm-github-bot commented on Nov 19, 2026

    @psalm-github-bot

    I found these snippets:

    https://psalm.dev/r/00cf304553
    <?php
    final class Bag
    {
        /** @var array<string, string> */
        private array $values;
    
        public function __construct(string $name)
        {
            $this->values['name'] = $name;
        }
    
        /** @return array<string, string> */
        public function all(): array
        {
            return $this->values;
        }
    }
    Psalm output (using commit f655225):
    
    ERROR: UninitializedProperty - 9:9 - Cannot use uninitialized property $this->values
    
    ERROR: PropertyNotSetInConstructor - 5:19 - Property Bag::$values is not defined in constructor of Bag or in any private or final methods called in the constructor
    
  2. added
    properties
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugproperties

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions