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?
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)
Is there a better way to handle this on PHP5.1 systems?