「php初心者」UnserializeとAutoloadを利用するサンプルコード
例1–unserialize関数
<?php
$string = 'O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$result = unserialize($string);
var_dump($result);
/*
object(__PHP_Incomplete_Class)[1]
public '__PHP_Incomplete_Class_Name’ => string 'Foostartnews24’ (length=6)
public 'foo’ => string '1’ (length=1)
public 'startnews24’ => string '2’ (length=1)
*/
?>
例2—spl_autoload_register関数
<?php
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
$string = 'O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$result = (array)unserialize($string);
var_dump($result);
?>
例3—FAKEクラスを利用
<?php
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
$string = 'O:6:“Foostartnews24”:2:{s:3:“foo”;s:1:“1”;s:3:“startnews24”;s:1:“2”;}’;
$functions = spl_autoload_functions();
foreach ($functions as $function) {
spl_autoload_unregister($function);
}
$result = (array)unserialize($string);
foreach ($functions as $function) {
spl_autoload_register($function);
}
var_dump($result);
?>