有没有一种简单的方法可以在 php 5.3 中模拟友谊
我需要一些课程来与系统中的其他课程交好。缺乏这个功能让我公开了一些不应该公开的方法。这样做的后果是我的团队成员以糟糕且丑陋的方式实现代码,从而导致混乱。
有没有办法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在寻求相同的功能并且能够实现一个解决方案。要使用它,只需从类 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
简而言之,不。普遍接受的方法是教育您的团队如何针对您的库进行开发或重新设计。第一个解决方案可以很容易地完成,方法是使用 phpdoc 构建文档并使用 @visibility 设置 docbloc 注释中的可见性,当然还可以实际记录类。如果不知道更多细节,我将无法评论第二个。
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.
我不确定你所说的“成为朋友”是什么意思。您可以使用抽象类,其中任何新类都可以“实现”该类。或者,您可以让类扩展其他类,并使方法、变量等“受保护”,而不是公共或私有。
如果您的团队“以糟糕且丑陋的方式”实现任何代码,那么您可能会遇到更大的问题。
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.