Voting

: four minus four?
(Example: nine)

The Note You're Voting On

m.m.j.kronenburg
8 years ago
You can use the following code to use the php 7 unserialize function in php 5.3 and upwards. This adds the $option argument.

<?php

namespace
{

/**
* PHP 7 unserialize function for PHP 5.3 upwards.
* Added the $option argument (allowed_classes).
* See php unserialize manual for more detail.
**/
function php7_unserialize($str, $options = array())
{
if(
version_compare(PHP_VERSION, '7.0.0', '>='))
{ return
unserialize($str, $options); }

$allowed_classes = isset($options['allowed_classes']) ?
$options['allowed_classes'] : true;
if(
is_array($allowed_classes) || !$allowed_classes)
{
$str = preg_replace_callback(
'/(?=^|:)(O|C):\d+:"([^"]*)":(\d+):{/',
function(
$matches) use ($allowed_classes)
{
if(
is_array($allowed_classes) &&
in_array($matches[2], $allowed_classes))
{ return
$matches[0]; }
else
{
return
$matches[1].':22:"__PHP_Incomplete_Class":'.
(
$matches[3] + 1).
':{s:27:"__PHP_Incomplete_Class_Name";'.
serialize($matches[2]);
}
},
$str
);
}
unset(
$allowed_classes);
return
unserialize($str);
}

}
// namespace

namespace my_name_space
{
/**
* Use the new php7 unserialize in your namespace without
* renaming all unserialize(...) function calls to
* php7_unserialize(...).
**/
function unserialize($str, $options = array())
{ return
php7_unserialize($str, $options); }
}

?>

<< Back to user notes page

To Top