简体   繁体   English

PHP:__callStatic替代5.2.x

[英]PHP: __callStatic alternative for 5.2.x

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. 所以我有托管拒绝更新到PHP 5.3(这很麻烦,但同时很好;我很灵活),但唯一让我感到惊讶的是没有__callStatic可用。

I have modules loaded that I'm wanting to consolidate down to a single class (ie 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. 我加载了模块,我想要合并到一个单独的类(即有一个加载模块的类,然后通过调用主类'__callStatic方法访问这些模块的函数),但是当我从本地上传时测试服务器到我的服务器,显然__callStatic方法不起作用。

Is there an alternative that is still static? 还有一种仍然是静态的替代方案吗? I like the clean code look for the double colon (ie registry::some_func() ) instead of $registry->some_func() . 我喜欢干净的代码查找双冒号(即registry::some_func() )而不是$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). 另一个缺点是我现在被迫在函数中使用global关键字(根据我自己的喜好,它非常非常混乱)。

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. 另外:我考虑过像往常一样用5.2编写__callStatic方法,然后显式调用方法,例如registry::__callStatic("some_func"), array("val", "val2", "val3")和保持事情更简单,甚至在升级到5.3时更容易迁移。 Thoughts on this method? 对这种方法的想法?

Thanks! 谢谢!

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. 你所说的“干净的代码外观”就像是Hammer的裤子......很酷的一年,几年之后,当你年龄更大,更聪明时,就不那么酷了。 __callStatic() (IMVHO) is an unnecessary minor convenience at best and a workaround for poor class design at worst. __callStatic() (IMVHO)充其量只是一种不必要的小便利,也是最糟糕的类别设计的解决方法。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM