"Friend" access for certain classes or interfaces
Posted by tcs-1986 3 years ago
PHP doesn't have a feature about "friend" access for certain classes or interfaces.
Certain classes should access private or protected methods in another "friendly" classes.
```php
class ABC {
friend A;
private $testPrivate = "private OK";
protected $testProtected = "protected OK";
public $testPublic = "public OK";
}
class A {
static function friend_test() {
$foo = new ABC();
echo $foo->$testPrivate;
echo $foo->$testProtected;
echo $foo->$testPublic;
}
}
class B {
static function friend_test() {
$foo = new ABC();
echo $foo->$testPrivate;
echo $foo->$testProtected;
echo $foo->$testPublic;
}
}
class C extends A {
// ...
}
A:friend_test();
// private OK
// protected OK
// public OK
B:friend_test();
// error: $test is a private method
// error: $test is a protected method
// public OK
C:friend_test();
// private OK
// protected OK
// public OK
```