There is an easy way to imitate friendship in php 5.3

I need some classes to be friends with other classes on my system. The lack of this feature forced me to publish some methods that should not be publicly available. The consequences of this are that my team members inject the code bad and ugly, which causes a mess.

Is there any way to define friendship in php 5.3?

(I know http://bugs.php.net/bug.php?id=34044 You can vote there if there is no easy solution).

+3
source share
3 answers

In short, no. The common approach is to either train your team on how to evolve against your libraries, or reverse engineer. The first solution can be made quite simply by creating documents with phpdoc and setting visibility in docbloc comments using @visibility and, of course, documenting classes. Secondly, I would not be able to comment without knowing a little more details.

/**
 * Some helper class for LibraryInterface
 *
 * ATTENTION!
 * This class should not be used outside this package.
 *
 * @visibility package
 * @package mypackage
 */
class Helper
{
  public function doStuff()
  {
    /* does stuff */
  }
}

/**
 * Class for accessing some part of the library.
 *
 * @visibility public
 * @package mypackage
 */
class LibraryInterface
{
  public function doStuff()
  {
    $this->helper->doStuff();
  }
}
0
source

I was looking for the same functionality and was able to implement a solution. To use it, simply deduce from the Friendship class and specify the classes that are friends of your derived class. You can find it here: http://rommelsantor.com/clog/2011/04/23/php-5-3-class-friendship-support

+3
source

, "". , "" . , , , .. "", .

If your team implements any bad and ugly code, then you can have big problems.

-1
source

Source: https://habr.com/ru/post/1747751/


All Articles