Unserialize and AutoLoad in PHP

Source: Internet
Author: User
Tags autoload
    1. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';

    2. $result = Unserialize ($string);
    3. Var_dump ($result);

    4. /*

    5. Object (__php_incomplete_class) [1]
    6. Public ' __php_incomplete_class_name ' = String ' Foobar ' (length=6)
    7. Public ' foo ' = String ' 1 ' (length=1)
    8. Public ' bar ' = = String ' 2 ' (length=1)
    9. */
    10. ?>
Copy Code

When deserializing an object, if the object's class definition does not exist, then PHP will introduce an unfinished class concept, namely: __php_incomplete_class, although we are successful deserialization, but still cannot access the data in the object, otherwise the following error message will appear: the Script tried to execute a method or access a property of a incomplete object. Please ensure that the class definition of the the object is trying to operate on is loaded _before_ unserialize () gets Called or provide a __autoload () function to load the class definition.

It's not a hard thing to do, just make a forced type conversion, and it becomes an array:

    1. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';

    2. $result = (array) unserialize ($string);
    3. Var_dump ($result);

    4. /*

    5. Array
    6. ' __php_incomplete_class_name ' = String ' Foobar ' (length=6)
    7. ' foo ' = String ' 1 ' (length=1)
    8. ' Bar ' = = String ' 2 ' (length=1)
    9. */
    10. ?>

Copy Code

However, if the system activates the autoload, the situation becomes more complicated. By the way: PHP actually provides a configuration option named Unserialize_callback_func, but the meaning and autoload almost, here is not introduced, we say autoload, examples are as follows:

    1. Spl_autoload_register (function ($name) {
    2. Var_dump ($name);
    3. });
    4. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';
    5. $result = (array) unserialize ($string);
    6. Var_dump ($result);
    7. ?>
Copy Code

Execution of the above code will find that Spl_autoload_register is triggered, most of the time this is meaningful, but if you encounter a poorly defined spl_autoload_register, such as the following code:

    1. Spl_autoload_register (function ($name) {
    2. Include "/path/to/{$name}.php";
    3. });
    4. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';
    5. $result = (array) unserialize ($string);
    6. Var_dump ($result);
    7. ?>
Copy Code

No doubt, because the class definition file is not found, so the error! Change Spl_autoload_register sure line, but the premise is you can change, if involved in third-party code, we can not take the liberty of the decision, at this time we need a way to let Unserialize to bypass AutoLoad, The simplest way is to fake the classes we need:

    1. Spl_autoload_register (function ($name) {
    2. Include "/path/to/{$name}.php";
    3. });
    4. Class Foobar {}//Oh, shit!
    5. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';
    6. $result = (array) unserialize ($string);
    7. Var_dump ($result);
    8. ?>
Copy Code

I have to say, the above code is really rubbish. To provide you with one person I wrote:

    1. Spl_autoload_register (function ($name) {

    2. Include "/path/to/{$name}.php";
    3. });

    4. $string = ' o:6: ' Foobar ': 2:{s:3: "foo"; s:1: "1"; S:3: "Bar"; s:1: "2";} ';

    5. $functions = Spl_autoload_functions ();
    6. foreach ($functions as $function) {
    7. Spl_autoload_unregister ($function);
    8. }

    9. $result = (array) unserialize ($string);

    10. foreach ($functions as $function) {

    11. Spl_autoload_register ($function);
    12. }
    13. Var_dump ($result);
    14. ?>
Copy Code

Although the code is a little bit more, but at least there is no fake class, it is more comfortable.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.