scottaubrey/befriender

A library to enable classes to allow select foreign classes and allow them to affect internal properties

0.1.2 2017-10-19 09:19 UTC

This package is not auto-updated.

Last update: 2024-05-04 22:01:58 UTC


README

A library to enable classes to allow select foreign classes and allow them to affect internal properties

Befriender gives a really simple API to allow specified classes access to the private state of other classes. It coes so through the magic methods __get, __set, etc.

Requirements

PHP 7.0+

How to use

use composer to install the library composer require scottaubrey/befriender.

Then add the Befriender trait to the class you want to share state, like so:

#!php
<?php
class Person
{
    use Befriender\Befriender;

    private $name;

    // the rest of the class
}

finally, call ::befriend at an appropriate point (most likely this is the point you construct your class), passing in the class to allow access as a string:

#!php
<?php
class Person
{
    use Befriender\Befriender;

    private $name;

    public function __construct()
    {
        $this->befriend(MyFriend::class);
    }
    // the rest of the class
}

There are full examples in the examples folder.

Disclaimer

I don't use this in production. You probably shouldn't either, and wait for PHP to get better state sharing, or design your classes around it.

If you know what this does though, it's nice not to maintain it yourself.

License

Befriender is licensed under the MIT License