$ this-> loss of value, well, its value

I have a problem with the PHP file that I am using and I cannot find a solution.

In one part of the code, the value for $this->value , and the value is set correctly in accordance with my testing.

However, later in the same code $this->value empty.

Here is 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 of the function __construct value is set, and I confirmed that this is happening.

However, in the bottom function function __toString , $this->value empty.

Any idea what could be causing this?

EDIT

So, to repeat, the value of $ this-> is correctly set in the __construct function, but empty in the __toString function.

EDIT 2

I should also mention that other variables set in the __construct function work in the __toString function, for example the $ this-> keyword. This means that the value of $ this-> is empty.

Edit 3 Class called so

 $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.' ), ); 
+6
source share
8 answers

This is because an unserialized object does not support methods if they do not know the type of the class.

When you do not serialize the result of calling get_keyword() , if the class of the serialized object is unknown, PHP will deviate from the special class __PHP_Incomplete_Class_Name . This class is basically a dummy class: it has no method. However, it does allow access to the attributes of a deserialized object.

So, if you want to be able to call the $this->value method (this is what you do in __toString ), you will need to include a file that declares the type $this->value before calling unserialize.

[edit] You can check out the PHP manual for more details on the non-serialization process.

+11
source

This is what causes the problems:

 $this->value = unserialize(get_option($keyword)); 

As noted earlier, you can update the original message with the actual declarations of the get_option() function and the class that wraps the get_alt_desc(), get_img_url(), get_web_url() .

It can be assumed that the get_option() function returns a serialized object that implements the following public methods:

 public function get_alt_desc() { /* custom code */ } public function get_img_url() { /* custom code */ } public function get_web_url() { /* custom code */ } 

By serializing and non-serializing your object, you lose access to all of the above methods. To avoid this, you need to change the get_option() function to return the actual object instead of the serialized view.

Assuming there is a Value class that represents your object:

 class Value { protected $alt_desc; protected $img_url; protected $web_url; public function __construct($keyword) { $this->alt_desc = $keyword[0]; // some string build around $keyword $this->img_url = $keyword[1]; // some string build around $keyword $this->web_url = $keyword[2]; // some string build around $keyword } public function get_alt_desc() { return $this->alt_desc; } public function get_img_url() { return $this->img_url; } public function get_web_url() { return $this->web_url; } } 

Then you can change your get_option() function to just return the object:

 function get_option($keyword) { /* * The code below is just an example * This function must return an object */ return new Value($keyword); } 

The constructor for Padd_Input_Advertisement should be updated as follows:

 function __construct($keyword,$name,$description='') { $this->keyword = $keyword; // removed the unserialize call as it not necessary anymore $this->value = get_option($keyword); $this->name = $name; $this->description = $description; } 
+4
source

I would add this as a comment, but I think I do not have reputation points to do this yet.

Have you tried to enable error reporting?

 error_reporting(E_ALL); ini_set("display_errors", 1); 
+1
source

In object-oriented programming, you should have a call function with $this . In your __toString() function, you have a direct call function.

So you called a class function like this

 $this->set_name(); 
+1
source

Using the Magaling theme from Padd Solutions , which contains a script for the question, I added the following code to index.php in the themes directory:

 /*********************************TEST***********************************/ $padd_options[ 'advertisements' ] = array( new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_468060_1', 'Banner Ad 1 (468x60)', 'The advertisement is placed beside the site name in the header.' ), new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_468060_2', 'Banner Ad 2 (468x60)', 'The advertisement is placed just below the title in Search Result page, Categories page, Tags page, Author page, and Archives page.' ), new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_125125_1', 'Square Ad 1 (125x125)', 'The advertisement will be posted at the side bar.' ), new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_125125_2', 'Square Ad 2 (125x125)', 'The advertisement will be posted at the side bar.' ), new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_125125_3', 'Square Ad 3 (125x125)', 'The advertisement will be posted at the side bar.' ), new Padd_Input_Advertisement( PADD_THEME_SLUG . '_ads_125125_4', 'Square Ad 4 (125x125)', 'The advertisement will be posted at the side bar.' ), ); echo var_dump( $padd_options[ 'advertisements' ] ); /*********************************TEST***********************************/ 

And I got the following result, which shows that it is an array with several other values:

 array (size=6) 0 => object(Padd_Input_Advertisement)[286] protected 'keyword' => string 'magaling_ads_468060_1' (length=21) protected 'value' => object(Padd_Advertisement)[282] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-468x060.gif' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Banner Ad 1 (468x60)' (length=20) protected 'description' => string 'The advertisement is placed beside the site name in the header.' (length=63) 1 => object(Padd_Input_Advertisement)[284] protected 'keyword' => string 'magaling_ads_468060_2' (length=21) protected 'value' => object(Padd_Advertisement)[283] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-468x060.gif' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Banner Ad 2 (468x60)' (length=20) protected 'description' => string 'The advertisement is placed just below the title in Search Result page, Categories page, Tags page, Author page, and Archives page.' (length=131) 2 => object(Padd_Input_Advertisement)[279] protected 'keyword' => string 'magaling_ads_125125_1' (length=21) protected 'value' => object(Padd_Advertisement)[278] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Square Ad 1 (125x125)' (length=21) protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 3 => object(Padd_Input_Advertisement)[277] protected 'keyword' => string 'magaling_ads_125125_2' (length=21) protected 'value' => object(Padd_Advertisement)[276] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Square Ad 2 (125x125)' (length=21) protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 4 => object(Padd_Input_Advertisement)[275] protected 'keyword' => string 'magaling_ads_125125_3' (length=21) protected 'value' => object(Padd_Advertisement)[273] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Square Ad 3 (125x125)' (length=21) protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 5 => object(Padd_Input_Advertisement)[217] protected 'keyword' => string 'magaling_ads_125125_4' (length=21) protected 'value' => object(Padd_Advertisement)[216] private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) private 'alt_desc' => string 'Padd Solutions' (length=14) private 'web_url' => string 'http://www.paddsolutions.com' (length=28) private 'target' => string '_new' (length=4) private 'css_class' => string '' (length=0) protected 'name' => string 'Square Ad 4 (125x125)' (length=21) protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 

I don’t know why the value in your script is lost, but there is something implicit or incorrect in the topic you use, or in how you call the function. The information provided is not enough, way.

Hope this helps.

+1
source

EDIT: Is an unserialise call needed at all:

 $this->value = unserialize(get_option($keyword)); 

get_option already returns an uncertified object (if update_option () was saved - which automatically serializes the object, if necessary, before saving it to the database)

ORIGINAL RESPONSE

I suspect the problem is with how you serialize the object.

You need to make sure that during the serialization process (conversion to string representation) the type of the serialized object is KNOWN. Only after that can you successfully cancel the initialization (convert back from the string representation to the object).

To answer another question regarding _toString() . This function is called magically PHP when you try to use your object in a string context (for example, when you try to echo an object echo). You DO NOT FIND this feature directly.

Here is one php file demonstrating WORKING flow; with comments on the parts where you should look for traps (causes of problems). Save it as index.php and run it to see the results.

 <?php error_reporting(E_ALL); ini_set('display_errors', '1'); 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; } } //Just a sample of the class representing your option object class Class_Of_Value { protected $alt_desc; protected $img_url; protected $web_url; public function __construct($keyword) { $this->alt_desc = 'description'; $this->img_url = 'image'; $this->web_url = 'web'; } public function get_alt_desc() { return $this->alt_desc; } public function get_img_url() { return $this->img_url; } public function get_web_url() { return $this->web_url; } } //Sample of a CORRECT get_option function. function get_option($keyword) { //THIS IS IMPORTANT $valueObject = new Class_Of_Value($keyword); //MAKE SURE THAT TYPE of serialised value is KNOWN return serialize($valueObject); } //Here is the root cause of your problem. //Wordpress' get_option() DOES NOT serialise returned objects. It returns just value of an option pulled from database! //Hence you can't call methods $this->value->get_alt_desc() etc. define ('PADD_NAME_SPACE', ''); //dummy definition of the constant for the sake of PHP Notices $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.' ), ); //Here __toString will be called by PHP outputing your HTML form filled with object values. echo($padd_options['advertisements'][0]); 

The recommended solution to your problem is to make sure that the Wordpress parameter is correctly serialized (with the type of the object) before being saved to the database (WPs update_option() ). So get_option will return a serialized string, which will then be correctly unesterified in your __construct method.

+1
source

Have you tried to force the operator -> priority?

I mean from this:

 $this->value->get_alt_desc() 

:

 ($this->value)->get_alt_desc() 

Maybe your PHP interpreter is trying to evaluate value->get_alt_desc()

+1
source

So, I figured out a solution to this problem that makes the code work properly. It does not resolve the root cause, but it is rather a workaround.

In the __construct function, I added:

 $this->value2 = $this->value; 

And then in the code that caused the error, I changed:

 $this->value->get_alt_desc() 

to

 $this->value2->get_alt_desc() 

So, all the code worked, except that one variable $ this-> value lost its value, and as soon as I used a different variable name, everything worked as it should.

-2
source

All Articles