Usage of friendly classes

Friendly classes see at least protected methods of eacg other. In PHP they’re implemented like the following:

[php]
<?php

abstract class Base
{
    protected function doit()
    {
        throw new \RuntimeException('Not implemented');
    }
}

class Closed extends Base
{
    protected function doit()
    {
        echo 'done!';
    }
}

class Opener extends Base
{
    public function execute(Closed $closed)
    {
        $closed->doit();
    }
}

$closed = new Closed();
$opener = new Opener();


$opener->execute($closed);

You can test it here and it is not a bug.

How is it used? In Yii 3 we are closing DI-container and event dispatcher to disallow configuring them in runtime while being able to configure them during application initialization withe the help of friendly-configurators.

We did it to prevent potentially harmful programming practices. The disadvantage is that the feature is not widely known and there are questions:

What do you think? Should friendly classes be used?

  • Yes, use friendly classes
  • No, do not use friendly classes

0 voters