Heim >Backend-Entwicklung >PHP-Tutorial >Call to a member function on a non-object,分不够再加
Call to a member function on a non-object,急在线等,分不够再加
calss cart {
function get_item()
{
if(!empty($_SESSION[ 'cart '])&&is_array($_SESSION[ 'cart ']))
{
return $_SESSION[ 'cart '];
}
else
{
$_SESSION[ 'cart ']=array();
return $_SESSION[ 'cart '];
}
}
}
$data=$cart-> get_item();
PHP Version 4.3.11,PHP Version 4.4.4 下测试出错,Call to a member function on a non-object
PHP Version 4.4.6, PHP Version 5.1.2 下测试正常,可以打印出print_r($data);
------解决方案--------------------
你最好还是 new 一个实例比较好.
------解决方案--------------------
能正常就怪了
class cart {
function get_item()
{
if(!empty($_SESSION[ 'cart '])&&is_array($_SESSION[ 'cart ']))
{
return $_SESSION[ 'cart '];
}
else
{
$_SESSION[ 'cart ']=array();
return $_SESSION[ 'cart '];
}
}
}
$cart = new cart();
$data = $cart-> get_item();
------解决方案--------------------
Call to a member function on a non-object
===========================================
呼叫空对象的成员函数.
"non-object ",说明对象未实例化,即是没有new过.
------解决方案--------------------
嗯,liyong98847()说的有道理.你检查一下php4下的php.ini里的register_globals是否为off,如果是on的话,$_SESSION[ 'cart ']与$cart是等效的.
在register_globals=on的情况下:
==============================================
$cart = new cart();//在cart类里改动了$_SESSION[ 'cart ']的值,也相当与改了$cart的值
$data=$cart-> get_item();//$cart已经被改动,造成Call to a member function on a non-object
你可以换个变量名试试,如果没发生错误,证明就是以上所说的问题
====================================================
$c = new cart();
$data=$c-> get_item();
------解决方案--------------------
LS的几位都说的很明白了。