强制访问__PHP_Incomplete_Class对象属性 [英] forcing access to __PHP_Incomplete_Class object properties
问题描述
我正在为php cms编写一个模块.在函数(回调)中,我可以访问来自框架代码的对象.
I'm writing a module for a php cms. In a function (a callback) I can access an object that comes from the framework code.
此对象的类型为__PHP_Incomplete_Class
,因为在会话开始之前不包括所需的头文件.如果不破解核心的cms代码,我将无法包含它.
This object is of type __PHP_Incomplete_Class
because the needed header file is not included before the session starts. I cannot include it without hacking the core cms code.
我想知道是否仍然可以访问对象属性(无法广播到数组).我问这个问题是因为我可以用var_dump()
看到值,但是使用$object->var
总是得到空值.
I wonder if is possibile to access the object properties anyway (casting to array does not work). I ask this because I can see the values with var_dump()
but using $object->var
I always get nulls.
推荐答案
当您取消序列化尚未包含的类的对象时,将出现此问题. 例如,如果您在加入该类之前调用session_start.
This issue appends when you un serialize an object of a class that hasn't been included yet. For exemple, if you call session_start before include the class.
不能直接访问PHPIncompleteClass对象,但是可以使用foreach,serialize和gettype. 用PHPIncompleteClass对象调用is_object将导致错误.
A PHPIncompleteClass object can't be accessed directly, but it's ok with foreach, serialize and gettype. Calling is_object with an PHPIncompleteClass object will result false.
因此,如果在会话中找到"__PHP_Incomplete_Class"对象,并且在session_load之后包含了类,则可以使用以下功能:
So, if you find a '__PHP_Incomplete_Class' object in your session and you've included your class after the session_load, you can use this function :
function fixObject (&$object)
{
if (!is_object ($object) && gettype ($object) == 'object')
return ($object = unserialize (serialize ($object)));
return $object;
}
这将产生可用的对象:
fixObject($_SESSION['member']);
这篇关于强制访问__PHP_Incomplete_Class对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!