Skip to content

Proxies - Access to private property from the same class scope (recursive) does not initialise Proxy #934

@olsavmic

Description

@olsavmic

Recently I came across an issue that was hard to debug and quite unexpected to me.

Code like the one below, which is fully valid PHP code

class Foo {
    /**
      * @ORM\ManyToOne()
      */
    private Foo $parent;

    ... // Always initialize $this->parent in constructor

    public function doSomething(): void {
         var_dump($parent->parent);
    }
}

will result in Typed property accessed before initialization error.

The situation is even worse with nullable properties which will simply return NULL which is pretty hard to spot.


I understand that it's an internal limitation of how currently proxy classes work BUT support for public properties has been added in 2.4 if I recall correctly.

I was not able to find this issue documented anywhere so I'd either:

  1. Add visible warning in the documentation to never access private properties directly from the same class.
  2. Handle private/protected properties the same way as public properties are currently handled

Also, since I've seen plans to move from custom implementation of Proxies to https://github.com/Ocramius/ProxyManager (doctrine/orm#8518), is it possible that this transition will resolve this issue?

Metadata

Metadata

Assignees

No one assigned

    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