myheritage / php-friendly
A utility for adding non-native class friendship capabilities to PHP applications
Installs: 13
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 17
Forks: 1
Open Issues: 0
pkg:composer/myheritage/php-friendly
Requires
- php: ^7.3
Requires (Dev)
- phpunit/phpunit: ^8.3
This package is not auto-updated.
Last update: 2025-12-10 16:09:39 UTC
README
A utility for adding non-native class friendship capabilities to PHP applications
Overview
The friend classes RFC suggested that friend classes would be a php-native feature. Since it was declined we've decided to develop our own non-native solution that solves the same problem - how to make multiple classes under the same namespace "friends", and let them call each-other's protected methods.
Requirements
- PHP >= 7.3
- php-unit >= 8.3
Composer Install
Add the dependency myheritage/php-friendly to your project if you use Composer to manage the dependencies of your project.
$ composer require myheritage/php-friendly
Usage example
Callee
Shared / Exposed functions must be annotated with the @friendly annotation
<?php
namespace MyHeritage\Friends\Tests\Friendly;
use MyHeritage\Friends\Friendly;
class AFriendlyClass
{
use Friendly;
/**
* @friendly
* @param $message
* @return mixed
*/
protected function friendlyMethod($message)
{
return $message;
}
protected function notFriendly()
{
return "Booo";
}
}
Caller
<?php
namespace MyHeritage\Friends\Tests\Friendly;
class MyClass
{
public function getHelpFromAFriendlyClass()
{
echo (new AFriendlyClass())->friendlyMethod('hello, can you give a hand?');
}
}