PHP4-未定义的类名

PHP4-未定义的类名,php,oop,class,php4,Php,Oop,Class,Php4,在PHP 4中,如果在定义类之前使用该类,则会出现以下错误: 致命错误:未定义的类名 “福”在 我的代码是: 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的人之外,

在PHP 4中,如果在定义类之前使用该类,则会出现以下错误:

致命错误:未定义的类名 “福”在

我的代码是:

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的人之外,还有什么解决方法吗?

在脚本开头的
require\u once()
d文件中定义您的类。

您可以使用:

call_user_func(array('foo', 'what'));

这将导致在运行时而不是编译时检查类/方法。

如果是php4,则可以使用测试来测试类的存在性。为了与php5兼容,您可以编写以下类型的代码:

<?php
function __autoload($classname) {
    include("classes/$classname.class.php");   
}

if (!class_exists('foo')) __autoload('foo');

不,升级到PHP5.3.5不管怎样,为什么要使用PHP4,我的意思是WTF已经过时了:有时候你只是被过时的技术卡住了……这看起来很难看。。我只是注意到函数也会发生这种情况。PHP 4是如此迟钝:(