源码如下:
class Safe
{
public function addslashes_deep($value)
{
$value = is_array($value) ? array_map(array(self,'addslashes_deep'),$value) : addslashes($value);
return $value;
}

public function cklogin($ses)
{
if(!isset($_SESSION[$ses]))
{
$this->warning('无效的访问,请先登录本系统!',"top.location.href='index.php';");
}
}

public function warning($msg,$do='')
{
echo " <html>
<head>
<meta http-equiv='content-type' content='text/html; charset=utf-8'>
<script language='javascript'>";
echo 'alert("'.$msg.'");';
echo $do;
echo '</script>';
echo ' </head>
<body>
</body>
</html>';
}
}
用 Safe::cklogin('user'); 调用之后,显示错误:
Fatal error: Using $this when not in object context in /usr/web/oa/OA/include/lib/inc.class.safe.php on line ...提示错误行代码是:
$this->warning('无效的访问,请先登录本系统!',"top.location.href='index.php';");
同样的问题,在我自己的机器上没有出现,但在公司的一台机子上去出现了,不知为什么,高人请教了!谢谢!
谢谢!

解决方案 »

  1.   

    没有实例是不允许调用类的成员变量或者成员函数的.
    正确的方式应该是
    $xxx = new Safe();
    $xxx->cklogin($ses);
      

  2.   

    我有用 Safe::cklogin($ses); 了啊。
      

  3.   

    Safe::cklogin($ses); 
    这样调用是不允许操作成员变量和成员函数的.
    你还是没有明白类.类没有实例化,如何能产生$this.
      

  4.   

    sorry!
    上面说法可能不太准备.正确的说法应该是,类没有被实例化之前,调用的成员函数内是不允许用$this来访问成员函数或者成员变量.
      

  5.   

    明白了,谢谢,我改用 function形式,或是 new 一个新类的实例,谢谢。但,原程序确实在我本机上运行通过的,怪呵呵,谢谢你,jakey9826 !
      

  6.   

    客气.本机上通过,奇怪,哪就不提而知了.
    难道是php版本不一样.
      

  7.   

    都是5.2.6,但有些配置参数不一样,公司机器是linux,少一些参数,没有配那么细.
    我个人是 windows ,郁闷,我也怪。哈哈,总之,还是非常感谢你!