Skip to content

get_object_vars leaks class in property when used for class with property getter #16725

Closed
@gielwijgergangs

Description

@gielwijgergangs

Description

The following code:

<?php

class Example implements JsonSerializable
{
    public function __construct(
        private array $authors,
    ) {}

    public string $credits {
        get {
            return implode(', ', $this->authors);
        }
    }
    
    public function jsonSerialize():mixed{
        return get_object_vars($this);
    }
}

$obj = new Example(['test', 'test2']);
echo json_encode($obj);
    

Resulted in this output:

{"\u0000Example\u0000authors":["test","test2"],"credits":"test, test2"}

But I expected this output instead:

{"authors":["test","test2"],"credits":"test, test2"}

Without the getter it does serialize as expected:

The following code:

<?php
class Example implements JsonSerializable
{
    public function __construct(
        private array $authors,
    ) {}

    public function jsonSerialize(): mixed
    {
        return get_object_vars($this);
    }
}

Results in

{"authors":["test","test2"]}

PHP Version

master

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions