PHP:5.2.x 的 __callStatic 替代方案

发布于 2024-10-10 22:15:47 字数 629 浏览 3 评论 0原文

所以我的主机拒绝更新到 PHP 5.3(这很烦人,但同时也很好;我很灵活),但唯一真正让我生气的是没有 __callStatic 可用。

我加载了模块,我想将其合并为一个类(即有一个加载模块的类,然后通过调用主类的 __callStatic 方法来访问这些模块的函数),但是当我从本地上传时在我的服务器上测试服务器,显然 __callStatic 方法不起作用。

是否有一个仍然静态的替代方案?我喜欢干净的代码查找双冒号(即 registry::some_func()),而不是 $registry->some_func()

另一个缺点是我现在被迫在函数中使用 global 关键字(根据我自己的喜好,这非常非常混乱)。

使用静态方法而不是非静态方法,代码也非常快,而且更加干净。

另外:我考虑过像往常一样使用 5.2 编写 __callStatic 方法,然后显式调用这些方法,例如 registry::__callStatic("some_func"), array("val" 、“val2”、“val3”) 并以这种方式让事情变得更简单,甚至在升级到 5.3 时更容易迁移。对这个方法的想法?

谢谢!

So I have hosting that refuses to update to PHP 5.3 (which is annoying but fine at the same time; I'm flexible), but the only thing that is really ticking me off is not having __callStatic available.

I have modules loaded that I'm wanting to consolidate down to a single class (i.e. have a class that loads modules, and then accesses those modules' functions through calls to the main class' __callStatic method), but when I uploaded from a local testing server to my server, obviously the __callStatic method didn't work.

Is there an alternative that is still static? I like the clean code look for the double colon (i.e. registry::some_func()) instead of $registry->some_func().

Another down-side to this is that I'm now forced to use the global keyword within functions (which by my own preference is very, very messy).

The code was also extremely fast using static methods instead of non-static methods, and in-turn much more clean.

Also: I've thought about writing the __callStatic method as usual with the 5.2 and then calling the methods explicitly, for example registry::__callStatic("some_func"), array("val", "val2", "val3") and keeping things simpler that way, even being easier to migrate when they do upgrade to 5.3. Thoughts on this method?

Thanks!

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

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

发布评论

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

评论(1

喵星人汪星人 2024-10-17 22:15:47

如果想要这样做的唯一原因是双冒号的速度和主观吸引力,那么我想说的是要欣赏破折号和括号的法则,大胆尝试并使用对象实例。你所说的“干净的代码外观”就像Hammer裤子一样......一年很酷,几年后当你年纪更大、更聪明时就不那么酷了。 __callStatic() (IMVHO) 充其量是一种不必要的次要便利,而最坏的情况则是不良类设计的解决方法。 :)

If the only reason for wanting to do this is speed and subjective appeal of the double-colon, I would say grow to appreciate the tao of the dash-and-bracket, take the plunge and use object instances. The "clean code look" you speak of is like Hammer pants... cool one year, not so cool a few years later when you are older and wiser. __callStatic() (IMVHO) is an unnecessary minor convenience at best and a workaround for poor class design at worst. :)

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