Skip to content

get_class_vars() function does not work correctly with promoted properties. #16546

@b-hayes

Description

@b-hayes

Description

The following code:

<?php
class TryMe
{
    public function __construct(
        protected string $id,
        protected string $name = 'default name',
    )
    {
        var_dump(get_class_vars(static::class)); die();
    }
}

new TryMe( 'something' );

Resulted in this output:

array(2) {
  ["id"]=>
  NULL
  ["name"]=>
  NULL
}

But I expected this output instead:

array(2) {
  ["id"]=>
  NULL
  ["name"]=>
  string(12) "default name"
}

I thought promoted properties were supposed to be the same as regular properties and just save us some writing for initializations but it introduced breaking changes.

PHP Version

PHP 8.1.30

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions