PHP:5.2.x 的 __callStatic 替代方案
所以我的主机拒绝更新到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果想要这样做的唯一原因是双冒号的速度和主观吸引力,那么我想说的是要欣赏破折号和括号的法则,大胆尝试并使用对象实例。你所说的“干净的代码外观”就像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. :)