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

Assign a object

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
gen
Smarty n00b


Joined: 12 Dec 2007
Posts: 2

PostPosted: Wed Dec 12, 2007 9:23 am    Post subject: Assign a object Reply with quote

in latest smary version can b assigned to template
array ,string, int and so on
Code:

    function assign($tpl_var, $value = null)
    {
        if (is_array($tpl_var)){
            foreach ($tpl_var as $key => $val) {
                if ($key != '') {
                    $this->_tpl_vars[$key] = $val;
                }
            }
        } else {
            if ($tpl_var != '')
                $this->_tpl_vars[$tpl_var] = $value;
        }
    }


but if i have a full Object Oriented PHP code, the assign funciton have no use fore me.

suggestion to modife the assign function


Code:

    function assign($tpl_var, $value = null)
    {
        if (is_array($tpl_var)){
            foreach ($tpl_var as $key => $val) {
                if ($key != '') {
                    $this->_tpl_vars[$key] = $val;
                }
            }
        }
        else if (is_object($tpl_var)){
           foreach (get_object_vars($tpl_var) as $key => $val) {
                if ($key != '') {
                    $this->_tpl_vars[$key] = $val;
                }
            }
        }
        else {
            if ($tpl_var != '')
                $this->_tpl_vars[$tpl_var] = $value;
        }
    }
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Dec 12, 2007 9:42 am    Post subject: Reply with quote

I don't think that's a good idea. To assign objects use register_object() or assign_by_ref().

FWIW, I think even assign() works fine with objects, even though they are copied eventually.
Boots, does COW in php apply to objects aswell? Smile

If I assign an object to Smarty, I certainly don't want all its object variables assigned individually. I want the whole object with all its variables and methods. And .. what if I assign two objects of the same class to Smarty? In your solution all the variables of the first object will be overwritten by the second one.
_________________
Darn computers always do what I tell them to instead of what I want them to do.
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 -> Feature Requests 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