$ this->价值损失,以及它的价值

She*_*ght 6 php variables

我有一个我正在使用的PHP文件的问题,我似乎无法找到解决方案.

在代码的一部分中$this->value设置值,并根据我的测试正确设置值.

但是,稍后在同一代码中$this->value是空的.

这是代码:

<?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;
    }

}

?>
Run Code Online (Sandbox Code Playgroud)

在顶部function __construct,设置值,我确认这发生了.

但是,在底部功能中function __toString,$this->value是空白的.

知道是什么原因引起的吗?

编辑

因此,回顾一下,$ this-> value在__construct函数中正确设置,但在__toString函数中是空白的.

编辑2

我还应该提到__construct函数中设置的其他变量在__toString函数中工作,如$ this-> keyword.这是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)',
        '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.'
    ),
);
Run Code Online (Sandbox Code Playgroud)

Maë*_*son 11

这是因为如果不知道类的类型,则序列化的对象不会保留方法.

当您反序列化get_keyword()函数调用的结果时,如果序列化对象的类未知,PHP将回退到特殊类__PHP_Incomplete_Class_Name.这个类基本上是一个虚拟类:它没有任何方法.但是,它允许您访问反序列化对象的属性.

因此,如果您希望能够调用$this->value(您正在进行的操作__toString)的方法,则必须$this->value在调用unserialize之前包含声明类型的文件.

[edit]您可以查看PHP手册,了解有关反序列化过程的更多详细信息.

  • 不,它们的创建方式与`$ this-> value`不同.后者是使用`unserialize`方法创建的,这是一个很大的区别. (2认同)

Dud*_*ock 1

我本来会将此添加为评论,但我想我还没有足够的声誉点来做到这一点。

您是否尝试过打开错误报告?

error_reporting(E_ALL);
ini_set("display_errors", 1);
Run Code Online (Sandbox Code Playgroud)