方法名称与语言结构冲突
我只想在我的类 list()
中命名一个方法。如果我尝试这样做:
class MyClass {
function list () {
// Do some stuff here
}
}
...我收到解析错误(Unexpected T_STRING...
)。如果我使用 echo
或 print
也是如此 - 所以我猜测我不允许将类方法命名为与语言构造相同的名称,并且 虽然我找不到任何明确说明这种情况的文档, 我认为这确实有道理。
我的问题是,有谁知道解决这个问题的方法,可以在 PHP 4.3.10 中工作? 我知道这可以在 PHP 5.3+ 中使用闭包来完成(或者我认为它可以,我还没有真正尝试过) 迄今为止没有任何 PHP 版本支持使用闭包来完成此操作,但是任何人都可以想到一个不依赖于此的解决方法,请记住这里重要的是方法name?
编辑 注意
我完全意识到 PHP4 是多么古老和消亡,但对于这个特定的项目我没有选择。它运行在 PHP 4.3.10 发布的平台上,这是一个资源非常低的 BusyBox 平台,带有 MIPS 处理器,没有提供编译器。我已经成功创建了一个交叉编译器并成功为其构建了 PHP 5.2(我还没有尝试过 5.3,它可能还涉及编译更新的 Apache),但无论如何,制造商坚持认为这会使他们的保修失效。
I simply want to name a method in my class list()
. If I try and do so:
class MyClass {
function list () {
// Do some stuff here
}
}
...I get a parse error (Unexpected T_STRING...
). The same is true if I use echo
or print
- so I am guessing that I am not allowed to name a class method the same as a language construct, and while I cannot find any documentation that explicitly states this to be the case, I suppose it does make sense.
My question is, does anyone know of a work around to this, that will work in PHP 4.3.10? I know this can be done in PHP 5.3+ with closures (or I assume it can, I haven't actually tried) No versions of PHP to date support doing this with closures, but can anyone think of a work-around that does not rely on this, baring in mind that it is the method name that is important here?
EDIT N.B.
I am fully aware of how ancient and dead PHP4 is, but I have no option for this particular project. It is to be run on a platform distributed with PHP 4.3.10, which is a very low resource BusyBox platform with a MIPS processor, for which no compilers are provided. I have managed to create a cross-compiler and successfully build PHP 5.2 for it (I haven't tried 5.3 yet, it would probably involve compiling a newer Apache as well) but regardless of this, the manufacturer insist that this invalidates their warranty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你面临的困境将一直持续到你选择A或B为止。
A:选择另一个函数名称,一个不是保留字的函数名称。
B:更改 PHP,使其提供您需要的语言功能。
__call
在 PHP 4 中不可用。list
是保留字 很久以来。而且您还没有概述类接口的具体问题是什么,因此我认为没有太多替代方案的空间。
由于 B 在这里看起来不像是一个真实的选项,因此您需要选择 A。
希望这有助于您得出结论。
The dilemma you face will stay until you choose A or B.
A: Choose another function name, one that is not a reserved word.
B: Change PHP so that it provides the language features you need.
__call
is not available in PHP 4.list
is a reserved word since long time.And you have not outlined what your specific problem with the class interface is, so I don't see much room for more alternatives.
As B does not look like a real option here, you need to take A.
Hope this helps that you can come to a conclusion.
保留关键字被保留:
因此请重命名您的方法。
Reserved keywords are reserved:
So rename your methods.
你有两个选择。
1)重命名你的方法。除非有真正的理由可以证明你的方法名称合理,否则这就是你应该做的。
2) 为你的类命名空间。如果将类包装在其自己的命名空间中,则可以重用通常保留或预定义函数或在其他地方定义的名称。然而,只有当您知道自己在做什么时才应该使用这种方法,否则最终可能会造成很多混乱。
我会选择选项 1,除非您有一个非常令人信服的理由为什么您的方法名称必须与 PHP 保留字具有相同的名称。
编辑:您使用的是 PHP 4,因此选项 2 不可行。您唯一的选择是为您的方法使用不同的名称。那,或者升级到 PHP 5.3。即使您最终不使用命名空间,我也强烈建议您升级到 PHP 5.3,因为 PHP 4 已经死了,而且已经死了很长一段时间了。不会再为其发布安全补丁,因此运行它基本上会使您的服务器变得非常不安全。
You've got two options.
1) rename your method. Unless there's a real reason you can justify your method's name then this is the one you should do.
2) namespace your class. If you wrap your class in its own namespace then you can reuse names that are normally reserved or predefined functions, or defined elsewhere. However you should only use this approach if you know what you're doing, otherwise you can end up causing a lot of confusion.
I'd opt for option 1 unless you have a really really compelling reason why your method name must have the same name as a PHP reserved word.
EDIT: You're using PHP 4 so option 2 is off the table. Your only choice is using a different name for your method. That, or upgrade to PHP 5.3. Even if you don't go with namespacing in the end I'd strongly advise upgrading to PHP 5.3 anyway, as PHP 4 is dead and has been for a long time. There will be no security patches released for it ever again so by running it you're basically making your server very insecure.
有点肮脏的黑客,但你可以使用魔法方法。 PHP4 并不像 php5 那样真正支持 __call,但它可以与“重载”结构一起使用。我还没有测试过这个,所以我不能确定..
( http://php .net/manual/en/function.overload.php)
Kind of a dirty hack, but you could use the magic methods. PHP4 doesn't really support the __call like php5 does, but it could work with the 'overload' construct. I haven't tested this so I can't be sure..
( http://php.net/manual/en/function.overload.php )