I have a problem with a PHP file I'm using, and I can't seem to find a solution.
In one part of the code the value for $this->value
is set, and the value is set correctly according to my testing.
However, later in the same code $this->value
is empty.
Here's the code:
<?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;
}
}
?>
At the top in function __construct
, the value is set, and I confirmed that this happens.
However, in the bottom function function __toString
, $this->value
is blank.
Any idea what could be causing this?
EDIT
So, to recap, $this->value is set properly in the __construct function, but is blank in the __toString function.
EDIT 2
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.
Edit 3 The class is called like this
$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.
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.
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] You can check the PHP manual for more details about the unserialization process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With