What is this __php_incomplete_class?

Asked

Viewed 1,570 times

5

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

What would be this mysterious class __PHP_Incomplete_Class?

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

Then, in my curiosity, I tried to instantiate the same.

$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 a Property of an incomplete Object. Please ensure that the class Definition "Unknown" 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 /var/www/lab/index.php on line 5

What would that mistake be?

What would that be __PHP_Incomplete_Class?

And where did that come from main in the Notice that was generated?

Updating:

Apart from everything that has been said before, there is another interesting question about __PHP_Incomplete_Class: The function is_object returns FALSE when we checked her out.

Behold:

var_dump(is_object(new __PHP_Incomplete_Class())); // (boolean) false

2 answers

6


Usually when trying to store objects in the session, in files or transmit them through sockets, the object can be referenced as being of the class __PHP_Incomplete_Class, this happens because the correct way to store and retrieve an object in the 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 session.auto_start disabled in php.ini), it is necessary to include the class definition before calling the function session_start().

  • Very enlightening! But I still find funny the way PHP handles some things :)

  • Not much standardization, not even of method names (snake_case and Camelcase)

  • 2

    Besides, I had to go to the ends of the earth to find this information

2

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

For example:

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

PHP will return the following:

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

Already with the class class X {} is stated in the document you used unserialize, would return that:

object(X)#202 (0) {
}

Care

You should take certain precautions when saving certain types of data in session. For example, imagine that you saved the user’s information logged in through an object:

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

If for some reason, due to a maintenance, you have changed the class name Usuario for UsuarioSite, for example, the user who has the saved session, prior to updating its maintenance, will have problems with the __php_incomplete_class, because there is no reference (class) Usuario.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.