Call to a member function on a non-object, not enough to add

Source: Internet
Author: User
Call to a member function on a non-object, emergency online, etc., not enough to add
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 test error, call to a member function on a non-object

PHP version 4.4.6, PHP version 5.1.2 Test Normal, you can print out the Print_r ($data);

------Solution--------------------
You'd better be a new instance.
------Solution--------------------
It's weird to be normal.


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 ();
------Solution--------------------
Call to a member function on a non-object
===========================================
Call a member function for an empty object.
"Non-object", stating that the object is not instantiated, that is, there is no new.
------Solution--------------------
Well, liyong98847 () makes sense. You check if the register_globals in PHP.ini under PhP4 is off, and if it is on, $_session[' cart ' is equivalent to $cart.

In the case of Register_globals=on:
==============================================
$cart = new Cart ()//change the value of $_session[' cart ' in the cart class, and change the value of $cart
$data = $cart-get_item ();//$cart has been altered, resulting in a member function on a non-object

You can change the name of the variable to try, if there is no error, proof is the above-mentioned problem
====================================================
$c = new cart ();
$data = $c-get_item ();

------Solution--------------------
Several of LS are very clear.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.