PHP problem with "__php_incomplete_class"

I’m developing a website with CodeIgniter and have created a User and a session:

$user->first_name = 'Gerep';
$user->gender = 'M';
$user->age = '26';


$this->session->set_userdata('user', $user);

But when I try to access the session object:

echo $this->session->userdata('user')->first_name;

It returns me a error: Object of class __PHP_Incomplete_Class could not be converted to string

I have always worked like that and never had that problem.

Thanks!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The class definition had not been loaded, when PHP tried to deserialize the object in the session.

You can solve your problem by employing Autoloading.

Solution 2

The solution works, but you need to ensure that the class object definitions are read before session:

$autoload['libraries'] = array('**our class**','session','form_validation');

Solution 3

The position of session_start() is important becouse script loaded from top to bothom. When you call it before spl_autoload session is loaded before cllass(Object) and does not know where to put data, so it’s been created __PHP_Incomplete_Class Object.
Before:

 session_start();

 spl_autoload_register(function($class){
     require_once 'classes/'.$class.'.php';
 });

Result: __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => User [id]…)
After:

 spl_autoload_register(function($class){
 require_once 'classes/'.$class.'.php';
 });

 session_start();

Result: User Object ( [id] =>…)

Solution 4

Look if you had any __autoload($class) and change it to use the spl_autoload_register() way. Example:

function __autoload($class)
{
    if (file_exists(APPPATH . 'core/' . $class  .EXT)) {
        require_once APPPATH . 'core/' . $class . EXT;
    } else {
        if (file_exists(APPPATH . 'libraries/' . $class . EXT)) {
            require_once APPPATH . 'libraries/' . $class . EXT;
        }
    }
}

would be changed to:

function CIautoload($class)
{
    if (file_exists(APPPATH . 'core/' . $class  .EXT)) {
        require_once APPPATH . 'core/' . $class . EXT;
    } else {
        if (file_exists(APPPATH . 'libraries/' . $class . EXT)) {
            require_once APPPATH . 'libraries/' . $class . EXT;
        }
    }
}

spl_autoload_register('CIautoload');

This way, you’ll be able of using all the PHP 5.3 power (and you won’t have problem with composer autoloads and CI, ;D)

Explanation

If after a while using PHP 5.2 you start to use PHP > 5.3 and all the OO way of coding, you will start to use spl_autoload_register. With CI, in projects with PHP 5.2, as you couldn’t use spl_autoload_register people used a known hack to autolad classes using a function __autoload($class) that they usually wrote on the config.php file.
Problem is when you mix both, the spl_autoload_register function will override your __autoload class and the error the question ask for will arise.

Solution 5

This happens to me recently, and reading the solutions give me an idea. The first problem is that the session_start was before than the autload, that’s the error. You must declare your autload or include this then you can do the session_start.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply