What is this class __php_incomplete_class?

4

I always end up discovering a lot of crazy things in this language that I love so much: PHP!

What would this mysterious class __PHP_Incomplete_Class ?

I accidentally "found" her when I gave her a get_declared_classes

Then, in my curiosity, I tried to instantiate it.

$incomplete = new __PHP_Incomplete_Class;

However, when I try to access or assign a value to any property, a Notice is generated:

$incomplete = new __PHP_Incomplete_Class;

$incomplete->test = 'teste';
  

Notice : main (): The script tried to execute a method or access to property of an incomplete object. Please ensure that the class definition "unknown" of the object you are trying to operate on is unserialize () gets called or provide a __autoload () function to load the class definition in / var / www /lab/index.php on line 5

What would be this error?

What would this __PHP_Incomplete_Class ?

And where did this main appear in the Notice that was generated?

Update:

In addition to everything that has been said before, there is yet another interesting question about __PHP_Incomplete_Class : The function is_object returns FALSE when we check it.

See:

var_dump(is_object(new __PHP_Incomplete_Class())); // (boolean) false
    
asked by anonymous 16.07.2015 / 17:44

2 answers

5

Generally when trying to store objects in the session, in files or pass them through sockets, the object can be referenced as being of the __PHP_Incomplete_Class class, this is because the correct way to store and retrieve an object in session and also in other cases) is to use the functions serialize() and unserialize() .

Note: It is interesting to note that in many cases (with the%% flag disabled in session.auto_start ), it is necessary to include the definition of the class before the call to the function php.ini .

    
16.07.2015 / 18:02
2

In addition to the above, it is important to remember that PHP returns a __php_incomplete_class instance when you call the unserialize function on a string that contains a serialization of a class that does not exist in the current context.

For example:

  • Create class X
  • Serialize its instance with serialize(new X) .
  • Copy the result of it to string . is something like O:1:"X":0:{} .
  • Delete the class X .
  • Call unserialize('O:1:"X":0:{}') , without class X being declared.

PHP will return the following:

object(__PHP_Incomplete_Class)#198 (1) {
  ["__PHP_Incomplete_Class_Name"]=>
  string(1) "X"
}

With class class X {} being declared in the document that used unserialize , this would be returned:

object(X)#202 (0) {
}

Care

You should take care when saving certain types of data in session. For example, suppose you saved the user information logged in through an object:

$_SESSION['usuario'] = Usuario::find(1); // retorna uma instância de Usuario

If for some reason, because of a maintenance, you have changed the name of the class Usuario to UsuarioSite , for example, the user who has the saved session, before updating your maintenance, will have problems with the __php_incomplete_class , due to non existence of the reference (class) Usuario .

    
14.08.2018 / 20:44