Yii Framework Forum: htmlspecialchars - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

htmlspecialchars htmlspecialchars() expects parameter 1 to be string, object given Rate Topic: ****- 1 Votes

#1 User is offline   amc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 13-March 09

Posted 31 January 2010 - 01:11 PM

Noted the following problem when I deployed an app created using Yii 1.1 running on PHP5.3.1 to a server running PHP5.1.6.

When using PHP5.3.1, if I pass a model to CHtml:encode e.g. CHtml::encode($model) and the model implements the __toString() method, then this is used to print the object. Yet when I deploy to PHP5.1.6 I get the following error

htmlspecialchars() expects parameter 1 to be string, object given


If I pass a normal attribute as e.g. CHtml::encode($model->attribute) then it works fine. However, if the model object happens to be null I will get the "trying to access property of non-object error. By simply passing the object to CHtml::encode it handled null relationships gracefully.

Whats the best way to handle model relationships which may be null when using CHtml::encode in a view?

amc.
0

#2 User is offline   amc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 13-March 09

Posted 31 January 2010 - 02:36 PM

View Postamc, on 31 January 2010 - 01:11 PM, said:

Noted the following problem when I deployed an app created using Yii 1.1 running on PHP5.3.1 to a server running PHP5.1.6.

When using PHP5.3.1, if I pass a model to CHtml:encode e.g. CHtml::encode($model) and the model implements the __toString() method, then this is used to print the object. Yet when I deploy to PHP5.1.6 I get the following error

htmlspecialchars() expects parameter 1 to be string, object given


If I pass a normal attribute as e.g. CHtml::encode($model->attribute) then it works fine. However, if the model object happens to be null I will get the "trying to access property of non-object error. By simply passing the object to CHtml::encode it handled null relationships gracefully.

Whats the best way to handle model relationships which may be null when using CHtml::encode in a view?

amc.


Found the problem.

Prior to PHP 5.2.0 the __toString method was only called when it was directly combined with echo() or print(). I want to retain the ability to pass a model object to CHtml::encode. I created my own encode method which verifies if parameter is not null and an object before invoking its __toString method

public static function encode($val)
  {
    if (isset($val))
      if (is_object($val))
         return CHtml::encode($val->__toString());
      else
         return CHtml::encode($val);
    else
      return '';
  }


Is there a better way to handle this on PHP5.1 systems?
0

#3 User is offline   aOmmy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-July 11

Posted 21 May 2012 - 06:34 AM

Quote

public static function encode($val)
{
if (isset($val))
if (is_object($val))
return CHtml::encode($val->__toString());
else
return CHtml::encode($val);
else
return '';
}

Thank you.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users