如何 PHP 将公共方法 ClassName.methodName() 传递给“unserialize_callback_func”什么时候调用`ini_set`?
作为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是这样完成的
This is how it is done
我认为您不能对对象的标准公共函数执行此操作,因为
unserialize_callback_func
是一个字符串设置,但是一个 回调是一个Array
。但是,如果将其设为静态公共函数,则可以将其作为字符串传递:
这可能是更好的选择。否则,您也可以将其包装到标准函数中:
另一种选择是,您使用 自动加载(例如
spl_autoload_register
)。
unserialize
支持自动加载,但我不知道自动加载是否可以帮助您你的情况经常如此。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 anArray
.But if you make it a
static
public function, you can pass it as a string:Which might be the better alternative. Otherwise you can wrap it into a standard function as well:
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.