有没有一种简单的方法可以在 php 5.3 中模拟友谊

发布于 2024-09-03 16:10:11 字数 265 浏览 2 评论 0原文

我需要一些课程来与系统中的其他课程交好。缺乏这个功能让我公开了一些不应该公开的方法。这样做的后果是我的团队成员以糟糕且丑陋的方式实现代码,从而导致混乱。

有没有办法在 php 5.3 中定义友谊?

(我知道 http://bugs.php.net/bug.php?id =34044如果没有简单的解决方案,您可能想在那里投票)。

I need some classes to befriend other classes in my system. Lack of this feature made me publicize some methods which shouldn't be public. The consequences of that are that members of my team implement code in a bad and ugly way which causes a mess.

Is there a way to define a friendship in php 5.3?

(I am aware of http://bugs.php.net/bug.php?id=34044 You might want to vote there if there is no simple solution).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清君侧 2024-09-10 16:10:11

我正在寻求相同的功能并且能够实现一个解决方案。要使用它,只需从类 Friendship 派生并指定属于派生类的友元类。您可以在这里找到它:http://rommelsantor .com/clog/2011/04/23/php-5-3-class-friendship-support

I was seeking the same functionality and was able to implement a solution. To use it, just derive from class Friendship 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

丶视觉 2024-09-10 16:10:11

简而言之,不。普遍接受的方法是教育您的团队如何针对您的库进行开发或重新设计。第一个解决方案可以很容易地完成,方法是使用 phpdoc 构建文档并使用 @visibility 设置 docbloc 注释中的可见性,当然还可以实际记录类。如果不知道更多细节,我将无法评论第二个。

/**
 * 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();
  }
}

In short, no. The generally accepted approach is to either educate your team on how to develop against your libraries or redesign. The first solution can be done quite easily by building docs with phpdoc and setting the visibility in the docbloc comments with @visibility, and of course actually documenting the classes. The second I wouldn't be able to comment on without knowing a little more detail.

/**
 * 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();
  }
}
雨夜星沙 2024-09-10 16:10:11

我不确定你所说的“成为朋友”是什么意思。您可以使用抽象类,其中任何新类都可以“实现”该类。或者,您可以让类扩展其他类,并使方法、变量等“受保护”,而不是公共或私有。

如果您的团队“以糟糕且丑陋的方式”实现任何代码,那么您可能会遇到更大的问题。

I'm not sure what you mean by "befriend". You can use abstract classes, in which any new class can "implement" that class. Or you can have classes extend other classes and make the methods, variables, etc "protected" as opposed to public or private.

If your team implements any code "in a bad and ugly way", then you may have bigger problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文