$this->value 损失,嗯,它的价值 [英] $this->value losing, well, its value
问题描述
我使用的 PHP 文件有问题,我似乎找不到解决方案.
I have a problem with a PHP file I'm using, and I can't seem to find a solution.
在代码的一部分中,设置了 $this->value
的值,并且根据我的测试正确设置了该值.
In one part of the code the value for $this->value
is set, and the value is set correctly according to my testing.
然而,在相同的代码中,$this->value
为空.
However, later in the same code $this->value
is empty.
代码如下:
<?php
class Padd_Input_Advertisement {
protected $keyword;
protected $value;
protected $name;
protected $description;
function __construct($keyword,$name,$description='') {
$this->keyword = $keyword;
$this->value = unserialize(get_option($keyword));
$this->name = $name;
$this->description = $description;
}
public function get_keyword() {
return $this->keyword;
}
public function set_keyword($keyword) {
$this->keyword = $keyword;
}
public function get_value() {
return $this->value;
}
public function set_value($value) {
$this->value = $value;
}
public function get_name() {
return $this->name;
}
public function set_name($name) {
$this->name = $name;
}
public function get_description() {
return $this->description;
}
public function set_description($description) {
$this->description = $description;
}
public function __toString() {
$strHTML = '';
$strHTML .= '<tr valign="top">';
$strHTML .= ' <th scope="row"><label for="' . $this->keyword . '">' . $this->name . '</label></th>';
$strHTML .= ' <td>';
$strHTML .= ' <label for="' . $this->keyword. '_alt_desc">Short Description</label><br />';
$strHTML .= ' <input name="' . $this->keyword . '_alt_desc" type="text" id="' . $this->keyword . '_alt_desc" value="' . $this->value->get_alt_desc() . '" size="80" /><br />';
$strHTML .= ' <label for="' . $this->keyword. '_img_url">Image URL</label><br />';
$strHTML .= ' <input name="' . $this->keyword . '_img_url" type="text" id="' . $this->keyword . '_img_url" value="' . $this->value->get_img_url() . '" size="80" /><br />';
$strHTML .= ' <label for="' . $this->keyword. '_web_url">Website</label><br />';
$strHTML .= ' <input name="' . $this->keyword . '_web_url" type="text" id="' . $this->keyword . '_web_url" value="' . $this->value->get_web_url() . '" size="80" />';
$strHTML .= ' <br /><small>' . $this->description . '</small>';
$strHTML .= ' </td>';
$strHTML .= '</tr>';
return $strHTML;
}
}
?>
在 function __construct
的顶部,设置了值,我确认发生了这种情况.
At the top in function __construct
, the value is set, and I confirmed that this happens.
然而,在底层函数function __toString
中,$this->value
为空.
However, in the bottom function function __toString
, $this->value
is blank.
知道是什么原因造成的吗?
Any idea what could be causing this?
编辑
所以,总结一下,$this->value 在 __construct 函数中设置正确,但在 __toString 函数中为空.
So, to recap, $this->value is set properly in the __construct function, but is blank in the __toString function.
编辑 2
我还应该提到在 __construct 函数中设置的其他变量也在 __toString 函数中起作用,例如 $this->keyword.只是 $this->value 变成空白.
I should also mention that other variables that are set in the __construct function are working in the __toString function, like $this->keyword. It's jut $this->value that is going blank.
编辑 3这个类是这样调用的
$padd_options['advertisements'] = array(
new Padd_Input_Advertisement(
PADD_NAME_SPACE . '_ads_125125_1',
'Square Ad 1 (125x125)',
'The advertisement will be posted at the side bar.'
),
new Padd_Input_Advertisement(
PADD_NAME_SPACE . '_ads_125125_2',
'Square Ad 2 (125x125)',
'The advertisement will be posted at the side bar.'
),
new Padd_Input_Advertisement(
PADD_NAME_SPACE . '_ads_125125_3',
'Square Ad 3 (125x125)',
'The advertisement will be posted at the side bar.'
),
new Padd_Input_Advertisement(
PADD_NAME_SPACE . '_ads_125125_4',
'Square Ad 4 (125x125)',
'The advertisement will be posted at the side bar.'
),
);
推荐答案
这是因为未序列化的对象在不知道类的类型时不保留方法.
It's because unserialized object does not keep methods if they do not know the type of the class.
当您对 get_keyword()
函数调用的结果进行反序列化时,如果序列化对象的类未知,PHP 将回退到特殊类 __PHP_Incomplete_Class_Name
.这个类基本上是一个虚拟类:它没有任何方法.但是,它允许您访问反序列化对象的属性.
When you unserialize the result of the get_keyword()
function call, if the class of the serialized object is not known, PHP will fallback on the special class __PHP_Incomplete_Class_Name
. This class is basically a dummy class : it doesn't have any method. However, it allows you to access to the attributes of the deserialized object.
因此,如果您希望能够调用 $this->value
的方法(这就是您在 __toString
中所做的),您将有在调用反序列化之前包含声明 $this->value
类型的文件.
So, if you want to be able to call the method of $this->value
(which is what you are doing in __toString
), you will have to include the file which declares the type of $this->value
before calling unserialize.
[edit] 您可以查看 PHP 手册 有关反序列化过程的更多详细信息.
[edit] You can check the PHP manual for more details about the unserialization process.
这篇关于$this->value 损失,嗯,它的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!