PHP 4 - 未定义的类名
在 PHP 4 中,如果在定义之前使用类,则会出现以下错误:
致命错误:未定义的类名 “foo”在...
我的代码是:
function do_stuff(){
if(foo::what()) ... // this code is before the php file with the foo class is included
}
class foo{
function what(){
...
}
}
do_stuff();
有没有解决方法(除了告诉使用你的脚本的人更新到 php5)?
In PHP 4, if you use a class before it's defined you get this error:
Fatal error: Undefined class name
'foo' in...
My code is:
function do_stuff(){
if(foo::what()) ... // this code is before the php file with the foo class is included
}
class foo{
function what(){
...
}
}
do_stuff();
is there any workaround for this (besides telling the people who use your script to update to php5) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以改为使用:
这将导致在运行时而不是编译时检查类/方法。
You could instead use:
which would cause the class/method to be checked at runtime rather than compile time.
在脚本开头的
require_once()
d 文件中定义您的类。Define your classes in a file which is
require_once()
d at the start of your script.如果是 php4,您可以使用
class_exists< 来测试类的存在/代码>
。所以为了兼容php5,可以写这样的代码:
if php4, you can test the existence of a class with
class_exists
. So to be compatible with php5, you can write this type of code :