Skip to content

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

Closed

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

Activity

  1. cmb69 commented on Nov 8, 2024

    @cmb69
    Member

    serialize() yields (

    O:7:"Example":2:{s:16:"Exampleauthors";a:2:{i:0;s:4:"test";i:1;s:5:"test2";}s:7:"credits";s:11:"test, test2";}
    

    so this is an engine issue, not JSON specific.

  2. ndossche commented on Nov 8, 2024

    @ndossche
    Member

    get_object_vars will call zho_build_properties_ex which normally uses the mangled names as keys.

  3. changed the title [-]json_encode leaks class in property when get_object_vars is used in jsonSerialize with property getter[/-] [+]``get_object_vars`` leaks class in property when get_object_vars used in class with property getter[/+] on Nov 9, 2024
  4. gielwijgergangs commented on Nov 9, 2024

    @gielwijgergangs
    Author

    @cmb69 you are right. The issue is not related to JSON, apologies for the confusion. I modified the title of the issue.

  5. changed the title [-]``get_object_vars`` leaks class in property when get_object_vars used in class with property getter[/-] [+]``get_object_vars`` leaks class in property when used for class with property getter[/+] on Nov 9, 2024
  6. cmb69 commented on Nov 9, 2024

    @cmb69
    Member

    Maybe @iluuu1994 can have a look?

  7. self-assigned this
    on Nov 9, 2024
  8. added a commit that references this issue on Nov 18, 2024
    048fa7b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions