DDC-883: "private" properties and proxy classes. #1097

Closed
opened 2026-01-22 13:01:57 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 18, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user fumanchu182:

When creating entities I usually create properties with the access level of private. However this prevents Proxy classes from retrieving data via **get or. For example the class

class Application*Entity*User {
    /*** @Id @Column(type="integer") @GeneratedValue **/
    private $id;

    public function **set($name, $value) {
        $this->$name = $value;
    }

    public function **get($name) {
        if (property_exists($this, $name)) {
            return $this->$name;
        }

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via **get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E*USER*NOTICE);
        return null;
    }
}

This generates a proxy class that has the following mapping for get:

class class Application*Entity_UserProxy extends \Application_Entity*User implements \Doctrine\ORM\Proxy\Proxy
{
    //..
    public function **get($name)
    {   
        $this->_load();
        return parent::**get($name);
    }

    //..
}

However the call from a proxy class to lets say $image->getUser()->id; will generate the following error:
Undefined property via __get(): id in /var/www/sites/foobar/library/Application/Entity/Proxy/Application_Entity_UserProxy.php on line

This is due to the access declaration in the original class. I filed this as a bug since I know a lot of the documentation has properties of entities defined as private. Changing those declarations to protected alleviates this issue and also does not introduce any security risks or break the OOP access security. With all this being said if I have missed documentation that says please make all entity properties "protected" ignore this and delete.

Originally created by @doctrinebot on GitHub (Nov 18, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user fumanchu182: When creating entities I usually create properties with the access level of private. However this prevents Proxy classes from retrieving data via **get or. For example the class ``` class Application*Entity*User { /*** @Id @Column(type="integer") @GeneratedValue **/ private $id; public function **set($name, $value) { $this->$name = $value; } public function **get($name) { if (property_exists($this, $name)) { return $this->$name; } $trace = debug_backtrace(); trigger_error( 'Undefined property via **get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E*USER*NOTICE); return null; } } ``` This generates a proxy class that has the following mapping for get: ``` class class Application*Entity_UserProxy extends \Application_Entity*User implements \Doctrine\ORM\Proxy\Proxy { //.. public function **get($name) { $this->_load(); return parent::**get($name); } //.. } ``` However the call from a proxy class to lets say $image->getUser()->id; will generate the following error: Undefined property via __get(): id in /var/www/sites/foobar/library/Application/Entity/Proxy/Application_Entity_UserProxy.php on line This is due to the access declaration in the original class. I filed this as a bug since I know a lot of the documentation has properties of entities defined as private. Changing those declarations to protected alleviates this issue and also does not introduce any security risks or break the OOP access security. With all this being said if I have missed documentation that says please make all entity properties "protected" ignore this and delete.
admin added the Bug label 2026-01-22 13:01:57 +01:00
admin closed this issue 2026-01-22 13:01:58 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 18, 2010):

Comment created by fumanchu182:

Removed personal site information in the text.

@doctrinebot commented on GitHub (Nov 18, 2010): Comment created by fumanchu182: Removed personal site information in the text.
Author
Owner

@doctrinebot commented on GitHub (Nov 19, 2010):

Comment created by @beberlei:

Since it calls parent::**get() i don't see why there should be a bug in the proxy. From my understanding of PHP OOP Model this should work with private properties.

@doctrinebot commented on GitHub (Nov 19, 2010): Comment created by @beberlei: Since it calls parent::**get() i don't see why there should be a bug in the proxy. From my understanding of PHP OOP Model this should work with private properties.
Author
Owner

@doctrinebot commented on GitHub (Nov 19, 2010):

Comment created by @beberlei:

Ah property_exists() is the problem, please try isset($this->$name) or empty.

@doctrinebot commented on GitHub (Nov 19, 2010): Comment created by @beberlei: Ah property_exists() is the problem, please try isset($this->$name) or empty.
Author
Owner

@doctrinebot commented on GitHub (Nov 19, 2010):

Comment created by fumanchu182:

I created a test entity (posted here) to test and everything seem complacent. If you would like to mark this issue as resolved I would agree that it has been addressed.

class A {
    private $x;
    private $y;
    private $z;

    public function **isset($name) {
        return isset($this->$name); 
    }

    public function **get($name) {
        if(isset($this->$name))
        {
            return $this->$name;
        }

        return null;
    }

    public function **set($name, $value) {
        $this->$name = $value;
    }
}
@doctrinebot commented on GitHub (Nov 19, 2010): Comment created by fumanchu182: I created a test entity (posted here) to test and everything seem complacent. If you would like to mark this issue as resolved I would agree that it has been addressed. ``` class A { private $x; private $y; private $z; public function **isset($name) { return isset($this->$name); } public function **get($name) { if(isset($this->$name)) { return $this->$name; } return null; } public function **set($name, $value) { $this->$name = $value; } } ```
Author
Owner

@doctrinebot commented on GitHub (Nov 22, 2010):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Nov 22, 2010): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1097