Sorry if this has been asked before but Googling didn’t give it to me.
How do you protect methods (or properties) from only unrelated classes, but have it available for specific, related classes?
class supplier {
protected person $contactPerson;
}
class unrelated {
function unrelatedStuff(supplier $supplier) {
$noneOfMyBusiness = $supplier->contactPerson; // should generate error
}
}
class order {
function __construct(readonly public supplier $supplier) {}
function informAboutDelivery() {
$contact = $this->supplier->contactPerson;
// should work (but doesn't) since to process an order you need the suppliers details
$contact->mail('It has been delivered');
}
}
I wrote a trait that can allow access to certain predefined classes to certain methods, whilst disallowing all other access, with a __method magic method and attributes. I can also think of using Reflection. But I feel that’s all overcomplicated and that I am missing something quite obvious here. Many classes are very closely related, others are unrelated. What is the normal way to deal with this? How do you shield information but not from all classes?