Unsure if this is working as intended or not. I have a class that uses both a __get and __set magic method and am using the [at]property doc-block code to define this. However to make things spicy, the __get returns a different item than accepted by the __set.

<?php
class Property {
    private $_value;

    public function __construct($value)
    {
        $this->_value = $value;
    }

    public function __get($item)
    {
        if ($item == 'value') return $this->_value;
    }

    public function __toString(): string
    {
        return $this->_value;
    }
}

/**
 * @property int $id
 * @property-write string $name
 * @property-read Property $name
 */
abstract class Account {
    private array $fields = [ 
        'id'   => null,
        'name' => '',
    ];

    public function __set(string $key, mixed $value)
    {
        $this->fields[ $key ] = $value;
    }

    public function __get($name)
    {
        switch ($name) {
            case 'id':
            case 'name':
                return new Property( $this->fields[ $name ] );

        }
        return $this->fields[ $name ];
    }
}

class DatabaseAccount extends Account {
    public function __construct()
    {
        $this->name = 'hello';

        var_dump( $this->name->value );
    }
}

This incorrectly hints that $this->name is a string. This also happens when moving the docblock to the DatabaseAccount class.

PeteAllison Thank you for the code sample and the detailed issue.

You are right, currently, we don't expect the two types may be different. We'll look into that.

    20 days later

    PeteAllison Fixed, the feature will be available in the next update. Thanks!

      Write a Reply...