Why is unserialize_callback_func required when spl_autoload_register is already in use?

ini_set('unserialize_callback_func', 'spl_autoload_call');

spl_autoload_register(array(self::getInstance(), 'autoload'));

Why install spl_autoload_callas above?

I did a test:

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

ini_set('unserialize_callback_func','mycallback');

function mycallback($classname) {
    echo 1;
}

function func2() 
{
    echo 2;
}

spl_autoload_register('func2');
unserialize($serialized_object);

Conclusion:

212

Can someone explain this?

+3
source share
2 answers

I did some tests, and here are the notes that I took (I hope that this will be clear ^^ ;; and that I was not too lost in my own thoughts ^^)
Note. I ran tests on PHP 5.3.2-dev, if that matters.


First of all, let it define a file temp-2.phpthat will contain only this:

<?php

class a {

}

i.e. a class definition that corresponds to the object that we will try to execute without initialization.

, , temp.php - temp-2.php, .


: a:

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
}

spl_autoload_register('callback_spl');
$data = unserialize($serialized_object);
var_dump($data);

:

string 'callback_spl : a' (length=16)

object(__PHP_Incomplete_Class)[1]
  public '__PHP_Incomplete_Class_Name' => string 'a' (length=1)
    public 'value' => string '100' (length=3)

, :

  • callback_spl
    • spl_autoload_register
    • .
  • , , , __PHP_Incomplete_Class


spl_autoload_register , :

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

spl_autoload_register('callback_spl');
$data = unserialize($serialized_object);
var_dump($data);

:

string 'callback_spl : a' (length=16)

object(a)[1]
  public 'value' => string '100' (length=3)

:

  • , spl_autoload_register
    • ,
    • . __PHP_Incomplete_Class,
    • a

, , unserialize_callback_func , spl_autoload_register.

, , - ? , ^^



, , unserialize_callback_func, spl_autoload_register?
, :

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

ini_set('unserialize_callback_func', 'callback_no_spl');

function callback_no_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

$data = unserialize($serialized_object);
var_dump($data);

, , :

string 'callback_no_spl : a' (length=19)

object(a)[1]
  public 'value' => string '100' (length=3)

, :

  • callback_no_spl, unserialize_callback_func
    • . a


, , , :

  • , callback_no_spl, unserialize_callback_func
  • , callback_spl, spl_autoload_register

:

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

ini_set('unserialize_callback_func', 'callback_no_spl');
function callback_no_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

spl_autoload_register('callback_spl');
function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

$data = unserialize($serialized_object);
var_dump($data);

:

string 'callback_spl : a' (length=16)

object(a)[1]
  public 'value' => string '100' (length=3)

:

  • , spl_autoload_register,
  • ,
  • .


, , , , ?
.. :

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

spl_autoload_register('callback_spl');
function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

ini_set('unserialize_callback_func', 'callback_no_spl');
function callback_no_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

$data = unserialize($serialized_object);
var_dump($data);

, :

string 'callback_spl : a' (length=16)

object(a)[1]
  public 'value' => string '100' (length=3)

, , , , spl_autoload_register , , unserialize_callback_func.


?
, , , spl_autoload_register (.. , ), :

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

ini_set('unserialize_callback_func', 'callback_no_spl');
function callback_no_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    require dirname(__FILE__) . '/temp-2.php';
}

spl_autoload_register('callback_spl');
function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    //require dirname(__FILE__) . '/temp-2.php';        // We don't load the class' definition
}

$data = unserialize($serialized_object);
var_dump($data);

:

string 'callback_spl : a' (length=16)

string 'callback_no_spl : a' (length=19)

object(a)[1]
  public 'value' => string '100' (length=3)

:

  • , spl_autoload_register,
  • , , unserialize_callback_func,
    • , , .


- , - , :

$serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';

ini_set('unserialize_callback_func', 'callback_no_spl');
function callback_no_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    //require dirname(__FILE__) . '/temp-2.php';        // We don't load the class' definition
}

spl_autoload_register('callback_spl');
function callback_spl($className)  {
    var_dump(__FUNCTION__ . ' : ' . $className);
    //require dirname(__FILE__) . '/temp-2.php';        // We don't load the class' definition
}

$data = unserialize($serialized_object);
var_dump($data);

, :

string 'callback_spl : a' (length=16)
string 'callback_no_spl : a' (length=19)
string 'callback_spl : a' (length=16)

( ! ) Warning: unserialize() [function.unserialize]: Function callback_no_spl() hasn't defined the class it was called for ...

object(__PHP_Incomplete_Class)[1]
  public '__PHP_Incomplete_Class_Name' => string 'a' (length=1)
  public 'value' => string '100' (length=3)

:

  • , spl_autoload_register,
  • , unserialize_callback_func,
    • 'defition...
  • , , spl_autoload_register, !
    • -
  • , , , unserialize_callback_func,
    • , callback_spl !
    • , -, , , , unserialize_callback_func, , ...

, - , , , , ...


, , :

"stack/queue" spl_autoload_register, , unserialize_callback_func...

+15

unserialize() , . unserialize_callback_func , , spl_autoload_call , , unserialize().

-1

Source: https://habr.com/ru/post/1734237/


All Articles