I know that private visibility in most of the OOP languages (if not all) define privacy in a class basis, i.e. different instances of the same class, can access private properties/methods of each other.
I want to prevent this and I want to know what is the best design/implementation in order to do this without a negative performance impact.
For example, I know that I could implement an AOP and use notations, but this would lead to a performance decrease since the languange engine would have to create the reflection of the class and check the annotation. So, basically, my question is, what is the best way to avoid instances of the same class to access each other's private methods/properties?
Example:
class Product { private $_prize; public function __construct($prize) { $this->_prize = $prize; } public function calculateDiscount(Product $extraProduct) { $extraProduct->_prize = 0; //How to avoid this? } } $productA = new Product(10); $productB = new Product(25); $productA->calculateDiscount($productB);