myheritage/php-friendly

A utility for adding non-native class friendship capabilities to PHP applications

v1.1.0 2020-03-31 08:19 UTC

This package is not auto-updated.

Last update: 2024-05-08 03:48:53 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?');
    }
}