PHP4 有依赖注入框架吗?

发布于 2024-07-05 17:45:08 字数 74 浏览 8 评论 0原文

我被困在 PHP 4 服务器上,我想开始将旧的遗留项目转移到现代设计模式,包括依赖注入。 是否有适用于 PHP 4 的依赖注入框架?

I'm stuck on a PHP 4 server, and I would like to start moving an old legacy project to modern Design Patterns, including Dependency Injection. Are there any dependency injection frameworks that will work with PHP 4?

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

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

发布评论

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

评论(3

无畏 2024-07-12 17:45:08

大多数依赖注入框架使用反射来确定依赖关系。 由于 PHP4 没有类型提示,因此您实际上无法执行此操作。 已经使用配置文件进行了实验 - 有时嵌入代码中的注释(通常称为注释)。 虽然这有效,但我发现它有点笨拙。 在我看来,您最好利用 PHP 的动态特性来发挥自己的优势,而不是尝试对其应用静态类型的解决方案。 手工工厂可以让你走得更远。 例如,请参阅这篇关于如何操作的文章

Most dependency injection frameworks use reflection to determine dependencies. Since PHP4 doesn't have typehints, you can't really do this. Experiments have been made with using config files - Some times embedded in comments in the code (Often called annotations). While this works, I find it a bit clunky. In my opinion, you're better off using PHP's dynamic nature to your advantage, than to try and apply statically typed solutions to it. You can get a long way with hand-crafted factories. See for example this post on how.

攒一口袋星星 2024-07-12 17:45:08

我找到了这个(drip),但看起来已经几年没有更新了。

i found this (drip), but it looks like it hasn't been updated in a few years.

ˇ宁静的妩媚 2024-07-12 17:45:08

我认为依赖注入框架不会真正在 PHP 上工作,因为它的面向对象程序的结构方式。 首先,它不像 C# 或 Java,其中二进制文件已经存在,您只需找到一种方法来实例化该对象并将其注入另一个对象。 PHP 必须先加载类文件并解释它们,然后才能使用它们。 因此,如果 PHP 有很深的继承层次结构,我认为这不是一个好主意。

鉴于 PHP 是一种脚本语言,因此最好将其用作脚本语言。 这意味着,我只会利用简单的工厂或构建器方法来执行类似于依赖项注入的操作。 我不会用 DI 框架来增加它的负担,该框架只会添加 PHP 运行时必须处理每个 Web 请求的内容(除非您进行操作码缓存,但仍然会产生 Java 和 .网)。 如果我必须更改将注入到对象中的对象或它们的创建方式,那么只需编辑包含工厂/构建器方法的脚本将是一个简单的任务。 无论如何都不需要重新编译。 所以我有灵活性,并且有一个适合 PHP 做事方式的轻量级架构。

I don't think a dependency injection framework will really work on PHP because of the way object-oriented programs are structured in it. First of all, it's not like C# or Java wherein the binaries are already there and you just have to find a way to instantiate this object and inject it into another. PHP has to load the class files and interpret them before it can use them. So if you have deep inheritance hierarchies with PHP I don't think that's a good idea.

Given that PHP is a scripting language, it's best to leverage it as that -- a scripting language. Which means, I would just make use of simple factory or builder methods to do something similar to dependency injection. I wouldn't burden it with a DI framework that will only add to the stuff the PHP runtime has to process for every web request (unless you do opcode caching, but there will still be overhead that's not incurred by web platforms for Java and .NET). If I have to change the objects that will be injected into objects or how they are created, it would be a simple task to just edit the script that contains the factory/builder methods. No need to recompile there anyway. So I have flexibility and I have a lightweight architecture that's suitable for the PHP way of doing things.

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