使用抽象类和静态成员更好地管理功能

发布于 2024-10-06 23:08:53 字数 649 浏览 5 评论 0原文

为了更好地组织 PHP 项目(一个简单 CMS)中的代码,我正在考虑将大部分系统函数作为静态成员移至抽象类。除了它在组织和语法上的好处之外,唯一的其他原因是将数据源对象等的引用存储为静态成员。

规则是在必要时被打破的,但我想巩固我对更好(阅读最佳)模式和实践的理解。

我想这个问题是开放式的,但我想知道是否有人有建议,或者可能建议一些阅读材料,这样我就可以探索我的选择以及什么被认为是“最佳实践”。

我的代码中的一个示例是管理权限的函数。对于任何给定的请求,可能需要进行权限检查,以确保发出请求的用户具有足够的操作权限。因此诸如 getAllPermissions()getGroupPermissions()addGroupPermissions() 等函数都在浮动。这些是否应该封装在实例化所需的 PermissionsManager 类中,如果是这样,我应该在哪里停止?我是否走在正确的道路上,将它们作为静态方法移动到抽象类中的伪全局空间?我应该将声明保留在全局范围内吗?适当的阶级责任在哪里结束,“神级”接管从哪里开始?我应该穿什么颜色的袜子?

我似乎无法理解这一点,它降低了我的工作效率。我不想再在建模上闲着了,因为尽管它有明显的好处,但我确实破坏了一些绘制对象交互图的树。我的废纸篓也满了。

In order to better organize my code in a PHP project (a simple CMS) I'm considering moving most of my System functions to an abstract class as static members. Aside from the organizational and syntactical benefit from it, the only other reason would be to store references to datasource objects, etc., as static members also.

Rules are made to be broken when necessary, but I want to solidify my understanding of better (read best) patterns and practices.

I suppose this question is open ended, but I was wondering if anyone had suggestions, or could perhaps suggest some reading material, so I could explore my options and what would be considered 'best practices'.

An example of this in my code would be functions for managing permissions. For any given request, permission checks may be necessary to ensure the requesting user has sufficient privileges for the operation. So functions such as getAllPermissions(), getGroupPermissions(), addGroupPermissions(), etc., are floating around. Should these be encapsulated within a PermissionsManager class, necessary to instantiate, and if so, where do I stop? Am I on the right track moving them to a pseudo-global space within an abstract class as static methods? Should I just leave the declarations in global scope? Where do appropriate class responsibilities end and 'god-class' takeovers begin? What color socks should I wear?

I just can't seem to wrap my head around this, and it's slowing my productivity. I don't want to idle any longer on modeling, because despite it's obvious benefits, I've certainly destroyed a few trees sketching out object-interaction diagrams. And my wastebasket is full.

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

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

发布评论

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

评论(2

ぇ气 2024-10-13 23:08:53

推荐一些阅读材料

看看 SOLIDGRASP 和我的这个答案:PHP 设计模式

这些是否应该封装在 PermissionsManager 类中

是。如果这些功能相关,请将它们分组。另请参阅有关基于角色的访问控制列表的维基百科文章 并查看 StackOverflow 上有关 PHP 中的 ACL 的各种问题。

我是否走在正确的道路上,将它们作为静态方法移动到抽象类中的伪全局空间?

不,如果可能的话,您应该避免静态。静态会导致紧密耦合并引入对全局范围的依赖,从而导致应用程序的可维护性降低,从而导致交付时间更长(例如错误修复和新功能),从而导致总体成本较高,投资回报较少。

我应该将声明保留在全局范围内吗?

如果您对申请进行更改,您应该能够解释这样做的好处。如果您认为您的应用程序可以从重构此代码中受益,那么重构它。当投资没有回报时就应该停止。

适当的阶级责任在哪里结束,“神级”接管从哪里开始?

由于您应该遵守单一责任原则,因此任何超出一项的责任都是代码异味(尽管有时拥有多个代码可能是有意义的)。我发现这个对GodClass的描述很容易理解。

我应该穿什么颜色的袜子?

这取决于您打算搭配的鞋子。总的来说,我发现深色袜子是大多数场合和鞋子的最佳选择,而 大多数时候白袜子是禁忌

我不想再闲着做模特了

。开始之前先思考一下是有好处的。了解问题和要解决的领域对于做出决策是必要的。但最终你应该从解决问题开始。只有有效的代码(即使是设计糟糕的代码)才能产生商业价值。无论如何,原始的前期设计无法在项目的现实中幸存下来。

suggest some reading material

Have a look at SOLID and GRASP and this answer of mine: Php design patterns

Should these be encapsulated within a PermissionsManager class

Yes. If these functions are related, group them. Also have a look at this Wikipedia Article about Role-Based Access Control Lists and have a look at the various questions about ACLs in PHP on StackOverflow.

Am I on the right track moving them to a pseudo-global space within an abstract class as static methods?

No, you should avoid static if possible. Statics lead to tight coupling and introduce dependencies on the global scope leading to less maintainable applications which leads to a longer time to deliver (e.g. bugfixes and new features) which leads to higher overall cost and less return of investment.

Should I just leave the declarations in global scope?

If you do changes to your application, you should be able to explain the benefit. If you think your application can benefit from refactoring this code, then refactor it. You should stop when there is no return of investment.

Where do appropriate class responsibilities end and 'god-class' takeovers begin?

Since you should stick to the single responsibility principle, any responsibility beyond one is a code smell (though sometimes it may make sense to have more than one). I found this description of a GodClass easy to comprehend.

What color socks should I wear?

That depends on the shoes you intend to wear with them. In general, I find dark socks to be the best choice for most occasions and shoes, while white socks are a No-Go most of time.

I don't want to idle any longer on modeling

Some thinking before you start is good. Understanding the problem and the domain you want to solve, is necessary to make decisions. But you should start with solving the problem eventually. Only working code (even poorly designed one) yields business value. Pristine up-front designs wont survive the reality of a project anyway.

素手挽清风 2024-10-13 23:08:53

作为一般建议,您应该不惜一切代价避免静态函数,至少,只要您遵循 OOP 路线。在 OOP 中,您的代码很像自然语言,函数调用遵循“主语-动词-宾语”短语结构:“某人做了某事(用其他事情)”。 “未绑定”(全局和静态)调用就像一个没有主语的短语,“这是用某物完成的”,这当然提出了一些直接的问题 - 谁是这个动作的执行者?造成的后果谁负责?为了测试目的,我们如何用其他东西替代这个动作?等等

当您遇到设计问题时,这种自然语言隐喻非常有帮助。用简单的短语写下您的问题。在主语、动词和宾语下划线。按照 $subject->verb($object) 模式用编程语言重写文本 - 瞧,你就完成了。

回到您的权限示例:您询问用户 $u 是否拥有实体 $e 的权限 $p。在自然语言中,您可以用三种不同的方式表达这一点,具体取决于您喜欢的主题:

  • “Joe read foo.txt?” (主题 = 用户)
  • “Joe 可以读取 foo.txt 吗?” (主题 = 实体)
  • 甚至“Joe 是否已授予 foo.txt 的读取权限?” (主题 = 权限)

这三种方式听起来都不错,并且可以立即转换为 OOP-php 代码:

if ($user->can_read($file)) ...
if ($file->is_readable_by($user)) ...
if ($read_permission->for_entity($file)->is_granted_to($user)) ...

不需要静态函数。

As a general advice, you should avoid static functions at any price, at least, as long as you follow the OOP route. In OOP, your code is much like a natural language, and function calls follow the "subject-verb-object" phrasal structure: "someone does something (with something else)". "Unbound" (global and static) calls are like a phrase without a subject, "this is being done with something", which, of course, raises some immediate questions - who is the actor of this action? who is responsible for the consequences? how can we substitute this action with something else, for testing purposes? etc etc

This natural language metaphor is extremely helpful when you run into design problems. Write down your problem in simple phrases. Underline subjects, verbs and objects. Rewrite the text in a programming language, following the $subject->verb($object) pattern - and voilà, you're done.

Back to your permissions example: you're asking if a user $u has a permission $p for an entity $e. In a natural language you can express this in three different ways, depending upon what you like to be a subject:

  • "Can Joe read foo.txt?" (subject = user)
  • "Is foo.txt readable by Joe?" (subject = entity)
  • and even "Is read permission granted to Joe for foo.txt?" (subject = permission)

All three ways sound fine, and can be immediately translated to OOP-php code:

if ($user->can_read($file)) ...
if ($file->is_readable_by($user)) ...
if ($read_permission->for_entity($file)->is_granted_to($user)) ...

No need for static functions.

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