Skip to content

Object Interfaces: Is the property really virtual? #4151

@mmalferov

Description

@mmalferov

Example #1 Interface properties example in the Properties section says that $writeable property is virtual:

This example creates a virtual property, which is fine

But the ReflectionProperty says that not.

<?php

interface I
{
    public string $readable { get; }
    public string $writeable { set; }
    public string $both { get; set; }
}

class C2 implements I
{
    private string $written = '';
    private string $all = '';

    public string $readable { get => strtoupper($this->writeable); }

    // The interface only requires the property be settable,
    // but also including get operations is entirely valid.
    // This example creates a virtual property, which is fine    →    is it correct?
    public string $writeable {
        get => $this->written;
        set => $value; // NB! A property is virtual if neither its get nor set hook references the
                       // property itself using exact syntax)
    }

    public string $both {
        get => $this->all;
        set => strtoupper($value);
    }
}

$c2 = new C2();
$reflector = new ReflectionProperty($c2, 'writeable');
var_dump($reflector->isVirtual()); // Returns false

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions