Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

On assigned objects and properties behaviour

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
corteccio
Smarty Rookie


Joined: 16 Jan 2010
Posts: 5

PostPosted: Sun Dec 05, 2010 6:19 pm    Post subject: On assigned objects and properties behaviour Reply with quote

Hello,

I know this has already bee discussed, but I'd like to point out the in my view inconsistent behavior of assigned objects properties handling in templates.

Assigning objects to templates is allowed and in Smarty 3 you can access public properties and call methods. As in:

Code:

class MyClass {

  public $pubprop;
  private $privprop;

  __construct() {
    $this->pubprop = "public";
    $this->privprop = "private";

  }

  public function setprivprop($value = "") {

    if(!empty($value))
      $this->privprop = $value;

    return $this->privprop;

  }

}

$smarty->assign('MyObj', new MyClass());



So in my template I can do:

Code:
pubprop: {$MyObj->public}
privprop: {$MyObj->setprivprop()}
privprop: {$MyObj->setprivprop('avalue')}


but not:

Code:
pubprop: {$MyObj->public}
{$MyObj->public = 'avalue'}
pubprop: {$MyObj->public}


This means I am allowed to read properties and call methods the class designer decided to make publicly available, but not set the value of public properties.

Now, I understand this is done to avoid people shooting themselves in the foot, But this also gets in the way of writing clean, generic classes and be able to let template designers easily customize results. It is not the designer responsibility to make sure this does not cause execution issues and you can circumvent this protection by adding a public setter method even for private properties. But doing the latter bloats the code.

If I want to shoot myself in the foot by allowing public properties to modify my code execution in bad ways I will anyway.

Now I can't set a simple label, but I am able to expose public methods for template designers which cause the end of the world as we know it... almost Wink
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP