如何 PHP 将公共方法 ClassName.methodName() 传递给“unserialize_callback_func”什么时候调用`ini_set`?

发布于 2024-12-13 01:19:29 字数 674 浏览 10 评论 0原文

作为 php 官方网站中的示例#2 .org,为了避免未知的类名,我们提供了对序列化类的定义的 include 调用。

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

// unserialize_callback_func directive available as of PHP 4.2.0
ini_set('unserialize_callback_func', 'mycallback'); // set your callback_function

function mycallback($classname) 
{
    // just include a file containing your classdefinition
    // you get $classname to figure out which classdefinition is required
}
?>

我的问题是,如果mycallback是C类的公共方法,我们如何在调用ini_set时将其作为参数传递?

As example #2 in this offical site from php.org, to avoid unkown classname, we provide include call to the definition of the serialized classes.

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

// unserialize_callback_func directive available as of PHP 4.2.0
ini_set('unserialize_callback_func', 'mycallback'); // set your callback_function

function mycallback($classname) 
{
    // just include a file containing your classdefinition
    // you get $classname to figure out which classdefinition is required
}
?>

My question is if mycallback is a public method of class C, how can we pass it as a parameter when calling ini_set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-12-20 01:19:29

是这样完成的

<?php 
class C {
    var $serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}'; 

    function mycallback($classname)  
    { 
        // just include a file containing your classdefinition 
        // you get $classname to figure out which classdefinition is required 
    } 

}

$c = new C();

// unserialize_callback_func directive available as of PHP 4.2.0 
ini_set('unserialize_callback_func', 'C::mycallback'); // set your callback_function 

unserialize($c->serialized_object);
?> 

This is how it is done

<?php 
class C {
    var $serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}'; 

    function mycallback($classname)  
    { 
        // just include a file containing your classdefinition 
        // you get $classname to figure out which classdefinition is required 
    } 

}

$c = new C();

// unserialize_callback_func directive available as of PHP 4.2.0 
ini_set('unserialize_callback_func', 'C::mycallback'); // set your callback_function 

unserialize($c->serialized_object);
?> 
半步萧音过轻尘 2024-12-20 01:19:29

我的问题是,如果mycallback是C类的公共方法,我们如何在调用ini_set时将其作为参数传递?

我认为您不能对对象的标准公共函数执行此操作,因为 unserialize_callback_func 是一个字符串设置,但是一个 回调是一个Array

但是,如果将其设为静态公共函数,则可以将其作为字符串传递:

class C
{
    public static function mycallback($classname) 
    {
        // just include a file containing your classdefinition
        // you get $classname to figure out which classdefinition is required
    }
}

ini_set('unserialize_callback_func', 'C::mycallback'); // set your callback_function

这可能是更好的选择。否则,您也可以将其包装到标准函数中:

function c_mycallback_wrapper($classname)
{
    $c = new C;
    return $c->mycallback($classname);
}
ini_set('unserialize_callback_func', 'c_mycallback_wrapper');

另一种选择是,您使用 自动加载(例如spl_autoload_register)。 unserialize 支持自动加载,但我不知道自动加载是否可以帮助您你的情况经常如此。

My question is if mycallback is a public method of class C, how can we pass it as a parameter when calling ini_set?

I think you can't do that for a standard public function of an object, because unserialize_callback_func is a string setting, but a callback of an object function is an Array.

But if you make it a static public function, you can pass it as a string:

class C
{
    public static function mycallback($classname) 
    {
        // just include a file containing your classdefinition
        // you get $classname to figure out which classdefinition is required
    }
}

ini_set('unserialize_callback_func', 'C::mycallback'); // set your callback_function

Which might be the better alternative. Otherwise you can wrap it into a standard function as well:

function c_mycallback_wrapper($classname)
{
    $c = new C;
    return $c->mycallback($classname);
}
ini_set('unserialize_callback_func', 'c_mycallback_wrapper');

Another alternative is, that you make use of autoloading (e.g. spl_autoload_register). unserialize supports autoloading, but I don't know if autoloading is something that helps you in your case, often it does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文