php – $this->值输,好吧,它的价值
我有一个我正在使用的
PHP文件的问题,我似乎无法找到解决方案.
在代码的一部分中,设置$this->值的值,并根据我的测试正确设置该值. 但是,稍后在同一代码中,$this->值为空. 这是代码: <?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; } } ?> 在函数__construct的顶部,设置了值,我确认会发生这种情况. 但是,在底部函数函数__toString中,$this->值为空. 知道是什么原因引起的吗? 编辑 因此,回顾一下,$this->值在__construct函数中正确设置,但在__toString函数中是空白的. 编辑2 我还应该提到__construct函数中设置的其他变量在__toString函数中工作,就像$this->关键字一样.这是jut $this->值空白. 编辑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)',new Padd_Input_Advertisement( PADD_NAME_SPACE . '_ads_125125_3','Square Ad 3 (125x125)',new Padd_Input_Advertisement( PADD_NAME_SPACE . '_ads_125125_4','Square Ad 4 (125x125)',);
这是因为如果不知道类的类型,则未序列化的对象不会保留方法.
当您反序列化get_keyword()函数调用的结果时,如果序列化对象的类未知,则PHP将回退到特殊类__PHP_Incomplete_Class_Name.这个类基本上是一个虚拟类:它没有任何方法.但是,它允许您访问反序列化对象的属性. 因此,如果您希望能够调用$this-> value的方法(这是您在__toString中执行的操作),则必须在调用之前包含声明$this->值类型的文件.反序列化. [edit]您可以查看the PHP manual以获取有关反序列化过程的更多详细信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |