-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Expose enumType to DBAL to make native DB Enum possible (#9382) * Accessing private properties and methods from the same class is forbidden (#9311) Resolves issue doctrine/common#934 Update docs/en/cookbook/accessing-private-properties-of-the-same-class-from-different-instance.rst Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com> Update docs/en/cookbook/accessing-private-properties-of-the-same-class-from-different-instance.rst Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com> Fix review issues * Update baselines for DBAL 3.3 (#9393) Co-authored-by: Vadim Borodavko <vadim.borodavko@gmail.com> Co-authored-by: olsavmic <molsavsky1@gmail.com>
- Loading branch information
Showing
6 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...book/accessing-private-properties-of-the-same-class-from-different-instance.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
Accessing private/protected properties/methods of the same class from different instance | ||
======================================================================================== | ||
|
||
.. sectionauthor:: Michael Olsavsky (olsavmic) | ||
|
||
As explained in the :doc:`restrictions for entity classes in the manual <../reference/architecture>`, | ||
it is dangerous to access private/protected properties of different entity instance of the same class because of lazy loading. | ||
|
||
The proxy instance that's injected instead of the real entity may not be initialized yet | ||
and therefore not contain expected data which may result in unexpected behavior. | ||
That's a limitation of current proxy implementation - only public methods automatically initialize proxies. | ||
|
||
It is usually preferable to use a public interface to manipulate the object from outside the `$this` | ||
context but it may not be convenient in some cases. The following example shows how to do it safely. | ||
|
||
Safely accessing private properties from different instance of the same class | ||
----------------------------------------------------------------------------- | ||
|
||
To safely access private property of different instance of the same class, make sure to initialise | ||
the proxy before use manually as follows: | ||
|
||
.. code-block:: php | ||
<?php | ||
use Doctrine\Common\Proxy\Proxy; | ||
use Doctrine\ORM\Mapping as ORM; | ||
/** | ||
* @ORM\Entity | ||
*/ | ||
class Entity | ||
{ | ||
// ... | ||
/** | ||
* @ORM\ManyToOne(targetEntity="Entity") | ||
* @ORM\JoinColumn(nullable=false) | ||
*/ | ||
private self $parent; | ||
/** | ||
* @ORM\Column(type="string", nullable=false) | ||
*/ | ||
private string $name; | ||
// ... | ||
public function doSomethingWithParent() | ||
{ | ||
// Always initializing the proxy before use | ||
if ($this->parent instanceof Proxy) { | ||
$this->parent->__load(); | ||
} | ||
// Accessing the `$this->parent->name` property without loading the proxy first | ||
// may throw error in case the Proxy has not been initialized yet. | ||
$this->parent->name; | ||
} | ||
public function doSomethingWithAnotherInstance(self $instance) | ||
{ | ||
// Always initializing the proxy before use | ||
if ($instance instanceof Proxy) { | ||
$instance->__load(); | ||
} | ||
// Accessing the `$instance->name` property without loading the proxy first | ||
// may throw error in case the Proxy has not been initialized yet. | ||
$instance->name; | ||
} | ||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters