3

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!

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

the session is been autoloaded on the autoload.php file, isn't that enogh?
I don't know what your script does. But what I do know is this: If you put an object into $_SESSION, PHP will serialize it to store it, and unserialize it when you access it again. If the class has not yet been parsed/loaded by PHP, you will get such an error. I suggest you check which object exactly is the culprit. If you var_dump() the object, you can see __PHP_Incomplete_Class on the problematic object.
Yeah...with var_dump I see the object as it needs to be but the type is __PHP_Incomplete_Class. I'm gonna check my classes again...
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');

1 Comment

Models cant load in libraries array
1

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] =>...)

Comments

0

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.

Comments

0

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.

1 Comment

I don't see how the placement of the session_start would cause the error he is reporting. Can you explain further?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.