The script tried to execute a method or access a property of an incomplete object

Posted on

The script tried to execute a method or access a property of an incomplete object – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and wordpress . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about The script tried to execute a method or access a property of an incomplete object.

I’m getting an error, the full error is:

Fatal error: authnet_cart_process() [<a href='function.authnet-cart-process'>function.authnet-cart-process</a>]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;AuthnetCart&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in /home/golfetc/public_html/wp-content/plugins/sccp-2.4.0/authnet_functions.php on line 1266

I’m using session to store cart object in it and get it later at some point. The authnetCart is basically class for cart object.

// Check cart in session
    if(isset($_SESSION['AUTHNET_CART'])) {
        // Get cart from session
        $authnetCart = $_SESSION['AUTHNET_CART'];
        foreach($authnetCart->getCartItems() as $item) {  // Line#1266
            if ($item->getItemId() == $subscription_details->ID ) {
                $addNewItem = false;
                break;
            }
        }
......

You can see at line 1266, the code doesn’t allow me to access its method. Any help will be highly appreciated. Thanks

Solution :

You need to include / require the php with your class BEFORE session_start() like

include PATH_TO_CLASS . 'AuthnetClassFilename.php';
session_start();

if (isset($_SESSION['AUTHNET_CART'])) {
    //...
}

It seems like your answer is in the error message.

Before unserializing AUTHNET_CART, include the class which defines it. Either manually, or using an autoloader.

include PATH_TO_CLASS . 'AuthnetClassFilename.php';

if(isset($_SESSION['AUTHNET_CART'])) {//...

It doesn’t appear that you’re actually unserializing it either (I’m assuming this was serialized before stuffing it into the session?)

if(isset($_SESSION['AUTHNET_CART'])) {
        // Get cart from session

        /** UNSERIALIZE **/
        $authnetCart = unserialize($_SESSION['AUTHNET_CART']);
        foreach($authnetCart->getCartItems() as $item) {  // Line#1266
            if ($item->getItemId() == $subscription_details->ID ) {
                $addNewItem = false;
                break;
            }
        }
...

None of the other answers in here actually solved this problem for me.

In this particular case I was using CodeIgniter and adding any of the following lines before the line that caused the error:

 $this->load->model('Authnet_Class');

OR

 get_instance()->load->model('Authnet_Class')

OR

 include APPPATH . '/model/Authnet_Class.php';

Did not solve the problem.

I managed to solve it by invoking the class definition in the construct of the class where I was accessing Authnet_Class. I.e.:

class MY_Current_Context_Class extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Authnet_Class');
    }
    // somewhere below in another function I access Authnet_Class ...

I now understand that the context where you access the Authnet_Class class, needs to have its definition present on the context’s class construct (and not just before you invoke the properties of Authnet_Class).

I do not recommend this technique, but there is a way to get around this error :

if( get_class($myObject)=='__PHP_Incomplete_Class' )
    $myObject = unserialize(preg_replace('/^O:d+:"[^"]++"/', 'O:'.strlen('MyClass').':"MyClass"', serialize($myObject)));

Having a good site architecture is obviously the right solution, but it can help temporarily until the problem is fixed

Leave a Reply

Your email address will not be published. Required fields are marked *