Does PHP Deploy Object Methods?

The PHP reference guides say that when serializing an object, the methods will not be saved. (See http://www.php.net/manual/en/language.oop5.serialization.php , paragraph 1).

However, the first example in the manual shows a method that is serialized, then non-esterized, and used.

Isn't that a contradiction? Did I miss something?

+4
source share
2 answers

I have to say that I do not see where the method is serialized in the first example. When serializing without methods, only the class name and properties are serialized. You can see this if you look at the serialized data.

$ser = serialize($object); var_dump($ser); 

You will notice that there is no method mentioned. However, if you do not serialize an object, you recreate it using the class name. Or in other words: you get a new object, but with the values ​​that you serialized earlier.

Usually this is not as important as it seems, because usually a serialized / uncertified object should behave the same.

 // serialize class A { public $a = null; public function test () { echo "Hello"; } } $a = new A; echo $a->test(); // "Hello" $x = serialize($a); // unserialize (somewhere else) class A { public $a = null; public function test () { echo "World"; } } $a = unserialize($x); echo $a->test(); // "World" 

Here the serializer uses the “wrong” class, and the result is different than expected. As long as you make sure there are no name conflicts, you usually don't need to think about it.

+10
source

The method is not serializable, but the class of which the member is a member is:

Methods in the object will not be saved, only the class name .

Therefore, when you non-serialize, you get an instance of the same class, so you can call the method on this non-certified instance, since this method is part of the class definition and not a member of the object itself. This, of course, assumes that you have the same class definition during non-serialization:

To be able to unserialize () an object, the class of this object must be defined.

+2
source

All Articles